2012-05-06 40 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的工作。