rails & devise, route to home#show after login using after_sign_in_path_for -
rails & devise, route to home#show after login using after_sign_in_path_for -
after sign-in, want route home#index, instead, generates /home/:id request , hence routed home#show.
can help , see, possible me route index after clicking sign in button.
thank much.
rake routes
prefix verb uri pattern controller#action new_user_session /users/sign_in(.:format) devise/sessions#new user_session post /users/sign_in(.:format) devise/sessions#create destroy_user_session delete /users/sign_out(.:format) devise/sessions#destroy user_password post /users/password(.:format) devise/passwords#create new_user_password /users/password/new(.:format) devise/passwords#new edit_user_password /users/password/edit(.:format) devise/passwords#edit patch /users/password(.:format) devise/passwords#update set /users/password(.:format) devise/passwords#update cancel_user_registration /users/cancel(.:format) devise/registrations#cancel user_registration post /users(.:format) devise/registrations#create new_user_registration /users/sign_up(.:format) devise/registrations#new edit_user_registration /users/edit(.:format) devise/registrations#edit patch /users(.:format) devise/registrations#update set /users(.:format) devise/registrations#update delete /users(.:format) devise/registrations#destroy posts /posts(.:format) posts#index post /posts(.:format) posts#create new_post /posts/new(.:format) posts#new edit_post /posts/:id/edit(.:format) posts#edit post /posts/:id(.:format) posts#show patch /posts/:id(.:format) posts#update set /posts/:id(.:format) posts#update delete /posts/:id(.:format) posts#destroy homes /homes(.:format) homes#index post /homes(.:format) homes#create new_home /homes/new(.:format) homes#new edit_home /homes/:id/edit(.:format) homes#edit home /homes/:id(.:format) homes#show patch /homes/:id(.:format) homes#update set /homes/:id(.:format) homes#update delete /homes/:id(.:format) homes#destroy root / devise/sessions#new
applicationcontroller.rb
class applicationcontroller < actioncontroller::base def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) } end protect_from_forgery with: :exception def after_sign_in_path_for(resource) home_path(resource) end def after_sign_out_path_for(resource) new_user_session_path() end end
sessions/new.html.erb
<%= form_for(resource, as: resource_name, url: session_path(resource_name), :html => {:class=>'form- signin'}) |f| %> <h2 class="form-signin-heading" style="text-align:center">log in</h2> <%= f.email_field :email, autofocus: true,:class=>"form-control", :type=>"email", :placeholder=>"enter email" %> <%= f.password_field :password, autocomplete: "off", :class=>"form-control", :type=>"password", :placeholder=>"password"%> <%= f.submit "submit", :class=>"btn btn-primary btn-block" %> <%= render "devise/shared/links" %> <% end %>
routes.rb
rails.application.routes.draw devise_for :users resources :posts resources :homes # routing login page devise_scope :user root :to => 'devise/sessions#new' end
homescontroller.rb
class homescontroller < applicationcontroller layout "loginpage" def index end def show @heading = "my home" end def new @home = home.new respond_with(@home) end def edit end def create @home = home.new(home_params) @home.save respond_with(@home) end def update @home.update(home_params) respond_with(@home) end def destroy @home.destroy respond_with(@home) end private def set_home @home = home.find(params[:id]) end def home_params params[:home] end end
you want after signin redirect homes#index
path on after_sign_in_path_for(resource)
method have home_path(resource)
redirect homes#show
look @ (your output of rake routes)
-> homes /homes(.:format) homes#index -> home /homes/:id(.:format) homes#show
homes_path
named helper path /homes
controller#action homes#index
home_path(id)
named helped path /home/:id
controller#action homes#show
http://guides.rubyonrails.org/routing.html#path-and-url-helpers
try :
if have multiple device models
class applicationcontroller < actioncontroller::base private def after_sign_in_path_for(resource) if resource.is_a?(yourdevicemodel) homes_path else another_path end end end
if have single device model
class applicationcontroller < actioncontroller::base private def after_sign_in_path_for(resource) homes_path end end
for question on comment
q: without s, route show method?
it may not so, have resources :homes
on routes.rb
can generate routing default rails. can read http://guides.rubyonrails.org/routing.html
ruby-on-rails ruby-on-rails-4
Comments
Post a Comment