【问题标题】:Ruby on Rails: 'show' route of accountRuby on Rails:“显示”帐户路径
【发布时间】:2020-05-30 03:40:35
【问题描述】:

目前我有两个模型,AccountUser。账号有很多用户,用户属于账号。我想显示帐户页面,但不断收到No route matches {:action=>"show", :controller=>"accounts"}, missing required keys: [:id] 错误。

Accounts控制器

def show
   @account = Account.find_by(params[:id])
end

帐户页面的链接

<%= link_to "Account Settings", account_path %>

路线

resources :accounts, only: [:new, :create, :show, :edit, :update]

【问题讨论】:

    标签: ruby-on-rails controller routes


    【解决方案1】:

    这里的问题:

    <%= link_to "Account Settings", account_path %>
    

    Rails 说 'missing required keys: [:id]' 这意味着你必须发送 id 来显示方法

    <%= link_to "Account Settings", account_path(123) %>
    

    当有人点击您的链接后,控制器将在此处获取此 ID:

    @account = Account.find_by(params[:id])
    

    将在数据库中找到您的记录,并将您重定向到:显示页面

    【讨论】:

    • 我修改了&lt;%= link_to "Account Settings", account_path(@current_user.account_id) %&gt;的链接,成功了!
    【解决方案2】:
    <%= link_to "Account Settings", account_path(@current_user.account_id) %>
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多