2014-10-18 59 views
0

登錄後,我想要路由到我的家庭#索引,但它會生成/ home /:id請求並因此路由到主頁#show 。rail&devise,route to home#使用after_sign_in_path_for登錄後顯示

有人可以幫忙看看,點擊登錄按鈕後,我是否可以路由到索引。

非常感謝。

耙路由

    Prefix Verb URI Pattern     Controller#Action 
    new_user_session GET /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 GET /users/password/new(.:format) devise/passwords#new 
    edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 
        PATCH /users/password(.:format)  devise/passwords#update 
        PUT /users/password(.:format)  devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)  devise/registrations#cancel 
    user_registration POST /users(.:format)    devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)  devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)   devise/registrations#edit 
        PATCH /users(.:format)    devise/registrations#update 
        PUT /users(.:format)    devise/registrations#update 
        DELETE /users(.:format)    devise/registrations#destroy 
       posts GET /posts(.:format)    posts#index 
        POST /posts(.:format)    posts#create 
      new_post GET /posts/new(.:format)   posts#new 
      edit_post GET /posts/:id/edit(.:format)  posts#edit 
       post GET /posts/:id(.:format)   posts#show 
        PATCH /posts/:id(.:format)   posts#update 
        PUT /posts/:id(.:format)   posts#update 
        DELETE /posts/:id(.:format)   posts#destroy 
       homes GET /homes(.:format)    homes#index 
        POST /homes(.:format)    homes#create 
      new_home GET /homes/new(.:format)   homes#new 
      edit_home GET /homes/:id/edit(.:format)  homes#edit 
       home GET /homes/:id(.:format)   homes#show 
        PATCH /homes/:id(.:format)   homes#update 
        PUT /homes/:id(.:format)   homes#update 
        DELETE /homes/:id(.:format)   homes#destroy 
       root GET /       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 

會話/ new.html.erb

<%= form_for(resource, as: resource_name, url: session_path(resource_name), :html => {:class=>'form- 
signin'}) do |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 do 
    devise_for :users 
    resources :posts 
    resources :homes 

    # routing to the login page 
    devise_scope :user do 
    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 
+0

嘗試與'homes_path' – 2014-10-18 03:01:35

+0

在applicationController? @anonymousxxx – 2014-10-18 03:04:04

+0

非常感謝你!!!!!!!!!它的工作原理..怎麼會... – 2014-10-18 03:06:33

回答

0

您想登入重定向後homes#index但你對after_sign_in_path_for(resource)方法路徑已home_path(resource)它會重定向到homes#show

看看這個(你的耙路輸出)

-> homes GET /homes(.:format)    homes#index 
-> home GET /homes/:id(.:format)   homes#show 

homes_path名爲幫手路徑/homes with controller#action is homes#index

home_path(id) is n艾湄有助於爲路徑/home/:id與控制器#動作homes#show

http://guides.rubyonrails.org/routing.html#path-and-url-helpers

試試這個:

如果您有多個設備型號

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 

如果你有一個設備型號

class ApplicationController < ActionController::Base 

    private 

    def after_sign_in_path_for(resource) 
     homes_path 
    end 

end 

對你的評論問題

問:沒有s,它會路由到顯示方法嗎?

它可能並不總是如此,你有resources :homes你的routes.rb它可以生成路由默認的軌道。您可以閱讀這http://guides.rubyonrails.org/routing.html

0

應用程序/控制器/ application_controller.rb

class ApplicationController < ActionController::Base 

    def after_sign_in_path_for(resource) 
    home_path 
    end 

end 

配置/ routes.rb中

Rails.application.routes.draw do 
    get 'home', to: "home#index", as: :home 
end 

應用程序/控制器/ home_controller.rb

class HomeController < ActionController::Base 
    def index 
    end 
end 

當你在Rails中指定一個靜態路由時,你可以說你想要使用哪個HTTP動作,然後是你想要轉發請求的控制器#動作,然後是你想要設置的_path名稱。

在你的情況下,你指定你想將登錄用戶發送到home_path,所以我們通過as:param設置home_path。