【问题标题】:Controller Show Action控制器显示动作
【发布时间】:2012-05-06 14:38:41
【问题描述】:

我有 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

结束

我的 rake 路线:

            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 的个人资料页面。

谢谢!

【问题讨论】:

  • 你能粘贴params吗?尝试从profile_path(@profile) 中删除@profile
  • 遇到同样的错误,抱歉,您指的是哪个params
  • params 散列,在这种情况下包含您通过 url 发送的所有参数 @profile。您应该发送@profile.id 并检查控制器是否命名为ProfileProfiles

标签: ruby ruby-on-rails-3


【解决方案1】:

看起来你没有在实际使用布局的动作中设置@profile,所以它出现为 nil,路由器需要一个 id 来生成路由。

您需要将@profile 设置为与应用程序布局一起呈现的任何操作中的某些内容。这可能是全局 before_filter 的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多