2012-05-06 43 views
1

我有2名型號的用戶(設計GEM)和配置文件,其中控制器顯示行動

class User < ActiveRecord::Base 
    has_one :profile 

    class Profile < AciveRecord::Base 
    belongs_to :user 

一切都在每個模型的意見工作正常,但我想在應用程序佈局的link_to。

<% if user_signed_in? %> 
    <li><%= link_to current_user.username , profile_path(@profile) %></li> 

但它表明我這個錯誤:

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"profiles"}) 

我的個人資料控制器

def show 
    @profile = Profile.find(params[:id]) 

respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @profile } 
end 

我耙路線:

  profiles GET /profiles(.:format)        profiles#index 
        POST /profiles(.:format)        profiles#create 
     new_profile GET /profiles/new(.:format)       profiles#new 
     edit_profile GET /profiles/:id/edit(.:format)     profiles#edit 
      profile GET /profiles/:id(.:format)       profiles#show 
        PUT /profiles/:id(.:format)       profiles#update 
        DELETE /profiles/:id(.:format)       profiles#destroy 
    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 
        PUT /users/password(.:format)           devise/passwords#update 

cancel_user_registration GET /users/cancel(.:format)     registrations#cancel 
     user_registration POST /users(.:format)       registrations#create 
    new_user_registration GET /users/sign_up(.:format)     registrations#new 
    edit_user_registration GET /users/edit(.:format)      registrations#edit 
          PUT /users(.:format)       registrations#update 
          DELETE /users(.:format)       registrations#destroy 

我希望鏈接顯示currentuser.username(我已經設置了用戶名)並鏈接到current_user的配置文件頁面。

謝謝!

+0

可以粘貼'params'嗎?嘗試從'profile_path(@profile)'中刪除'@ profile' –

+0

獲取相同的錯誤,不好意思,但是你的意思是哪個'params'? –

+0

'params'哈希,它具有您在這種情況下通過URL發送的所有參數'@ profile'。您應該發送'@ profile.id'並檢查控制器是否被命名爲'Profile'或'Profiles' –

回答

0

看起來好像你沒有在實際使用佈局的動作中設置@profile,所以它會變成零,路由器需要一個ID來生成路由。

您需要將@profile設置爲與應用程序佈局一起呈現的任何操作中的某個內容。這可能是全球before_filter的工作。