代码之家  ›  专栏  ›  技术社区  ›  calyxofheld

链接到错误模型的rails中的嵌套路由

  •  0
  • calyxofheld  · 技术社区  · 4 年前

    如果我的路线像这样

    get 'users/:id/doc/:id', to: 'users/docs#show', as: :show_doc
    

    def show
     user = User.find(params[:id]) #in this case it should be user id 6
     doc = Doc.find(params[:doc_id]) #in this case it should be doc id 29
    end
    

    如果索引视图中的链接是这样的

    <% user.docs.each do |i| %>
     <%= link_to "#{i.id}", show_doc_path(user.id, i.id), target: :_blank %>
    <% end %>
    

    localhost:3000/users/29/doc/29 ?

    1 回复  |  直到 4 年前
        1
  •  1
  •   Arun Kumar Mohan    4 年前

    问题是路由定义使用相同的密钥( id doc_id 文件id和你应该很好去。

    get 'users/:id/doc/:doc_id', to: 'users/docs#show', as: :show_doc