Devise and current_page? - ruby

In my Rails 4 app i use devise to authenticate my users and i've setup a custom nav bar creator using HAML and the application_helper.rb. The code in the helper uses current_page to decide where to add an 'active' class to the HTML element because im using the twitter bootstrap. Now everything i've said works fine such as when you go to the root of the site the button in the bar is active (pushed in) but if you click on another item in the bar and go to a pag such as /sig_in/ you get sent to the page but the button for that page is not active. Im not sure wether this is a devise or ruby problem. Here is my code:
routes.rb
devise_for :users, :path => ''
_nav.html.haml
= navigation_link_to 'Home', root_path
/ Insert Additional Nav
= navigation_link_to 'Sign in', new_user_session_path
application_helper.rb
def navigation_link_to(text, path)
link_to text, path, class: "btn#{active_link(path)}"
end
def active_link(path)
' active' if current_page?(path)
end

For me this is working quite well (I use 3.0.0.rc devise gem in Rails 4)
In routes.rb only:
devise_for :users
I have a _login.html.haml which is inserted in _navigation.html.haml. Path helpers provided by Devise automatically. If user already signed in then different links will be appeared.
%li=link_to 'Registration', new_user_registration_path
%li=link_to 'Sign in', new_user_session_path unless user_signed_in?
-if user_signed_in?
%li=link_to 'Manage your registration', edit_user_registration_path
%li=link_to 'Logout', destroy_user_session_path, method: :delete

Related

How to implement "change your profile picture" feature (server side)

Im trying to implement a feature on my project, so users can change their profile picture. I'm currently at the point where everything on the front end works (when you hover the mouse over the image it shows camera and when you click it, you can browse through your computer to select an image file) Yet, it doesnt work after that point.
I'm not really sure how to handle server request.
FYI
im using routes.rb and controller .rb files to handle backend.
Also,
should i use iframe, instead of form tag in html?
Thanks!
You don't need an iFrame. Simple form-for ruby helper will help in this case. I will suggest you to use a file-uploading gem like paperclip.
Create a model for your records:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
(I am supposing the model name to be user)
Create a new migration with generator:
rails generate paperclip user avatar
Update your html form as follow:
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
Then do following changes in the controller:
def create
#user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:avatar)
end
To display image anyware on the page use the following helper:
<%= image_tag #user.avatar.url %>
This is the easiest way to handle image uploading at server side in rails. You can read more at the paperclip github page.If you don't want to use a gem then then there is a ruby class FileUtils which can help you in achieving same goal.

Error while trying to delete a session in Rails

New to Rails. I have a model named Admin in my rails application. when i tried to delete the session i am getting No route Match Error.
I have created session controller to manage sessions shown below
class AdminSessionsController < ApplicationController
before_action :confirm_logged_in, :except => [:new, :create, :destroy]
def destroy
session[:admin_id] = nil
flash[:notice] = "You are now logged out"
render("new")
end
end
Routes I defined in routes.rb
resources :admin_sessions, only: [:new, :create, :destroy]
I have link in some other view to delete a session
<%= link_to "Sign out", :controller => "admin_sessions", :action => "destroy" %>
And I am getting the following Error
No route matches {:action=>"destroy", :controller=>"admin_sessions"}
Please help somebody!
You define resources while it's resource. You do not depend on specific id, so it always should respond with one resource (DELETE /admin_sessions/id vs DELETE /admin_sessions). Change in your route resources to resource and it will work.

How can I create a new layout and have it show up only in certain situations using ruby padrino?

I'm using padrino with my website, and need to override the default layout to use a different layout in certain situations i.e. when a user clicks on a certain thing.
Padrino documentation gives these examples:
SimpleApp.controllers :posts do
# Apply a layout for routes in this controller
# Layout file would be in 'app/views/layouts/posts.haml'
layout :posts
get("/posts") { render :haml, "Uses posts layout" }
end
SimpleApp.controllers :admin do
get :index do
render "admin/index", :layout => :admin
end
get :show, :with => :id do
render "admin/show", :layout => false
end
end

Get Rails to hit controller with an input tag submit

View:
%input{type: 'submit', action: 'home#create_user'}
Controller:
class HomeController < ApplicationController
def index
render 'home/index'
end
def sign_up
render 'home/sign_up'
end
def create_user
render 'dashboard/dashboard'
end
end
routes.rb
post 'home/create_user' => 'home#create_user', :as => :create_user
Why is this button not hitting the controller?
By itself, an submit tag isn't going to generate the form. I tried your code above and the button doesn't do anything. Unless I'm forgetting INPUT doesn't have an ACTION attribute.
If you had a link to that page, the reason it's not working is because by default that link will be a GET request and you've restricted the route to POST.
So, either wrap it up in a form or use button_to or the :method => :post solutions to make it POST the request and it should work.
Something like this:
= button_to 'click me', create_user_path
= link_to 'click me', create_user_path, method: 'post'

How to setup a rails 3 route for a nested resource and custom controller action

I have a recommendation that is nested below Categories and Awards.
So Category/:id/awards/:id/recommendations/:id
I have an Assets model that handles paperclip attachments to the Recommendation. A Recommendation has_many Assets, Assets belong_to :recommendation
In my Recommendation new/edit views I am rendering a form partial (as is normal) that gives the user the option to upload several assets.
If there are Assets already related, then it lists them. I am working on setting up a custom delete action on the recommendations controller.
My current link_to:
<%= link_to "Delete Attachment",
{:controller => :recommendations, :action => :destroy_asset, :id => asset.id },
{:remote => "true", :confirm => "Are you sure you want to delete this image?"}
%>
My Controller action:
def destroy_asset
##recommendation = Recommendation.find(params[:id])
#asset = Asset.find(params[:id])
#asset.destroy
respond_to do |format|
format.js
end
end
routes:
resources :recommendations
resources :categories do
resources :awards do
resources :recommendations
end
end
I am still learning remote => true, and how to route this sucker. Not sure if I need to have the route nested or not. I tend to think not. Since I have an Asset.id in my loop, I should just be able to execute the destroy without needing the Recommendation at all.
So the question/s: do I need a route to access a custom action in my recommendations_controller?
thanks
Try this:
resources :categories do
resources :awards do
resources :recommendations
member do
get :destroy_asset
end
end
end
end
Or If you only want only destroy_asset action under recommendation controller, do something like this :
resources :recommendations
member do
get :destroy_asset
end
end
Or If you want custom match, do something like this :
match "asset/:id/destroy_asset", :to => "recommendations#destroy_asset",
:as=> "destroy_asset"

Resources