加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为嵌套资源的“显示”和“编辑”操作指定正确

发布时间:2020-12-17 03:47:58 所属栏目:百科 来源:网络整理
导读:我有2个型号 father.rb: class Father ActiveRecord::Base has_many :sons,dependent: :destroyend son.rb class Son ActiveRecord::Base belongs_to :fatherend 的routes.rb Family::Application.routes.draw do resources :fathers do resources :sons en
我有2个型号

father.rb:

class Father < ActiveRecord::Base
    has_many :sons,dependent: :destroy
end

son.rb

class Son < ActiveRecord::Base
    belongs_to :father
end

的routes.rb

Family::Application.routes.draw do
  resources :fathers do
    resources :sons
  end
end

rake路线输出:

father_sons GET    /fathers/:father_id/sons(.:format)          sons#index
                POST   /fathers/:father_id/sons(.:format)          sons#create
 new_father_son GET    /fathers/:father_id/sons/new(.:format)      sons#new
edit_father_son GET    /fathers/:father_id/sons/:id/edit(.:format) sons#edit
     father_son GET    /fathers/:father_id/sons/:id(.:format)      sons#show
                PATCH  /fathers/:father_id/sons/:id(.:format)      sons#update
                PUT    /fathers/:father_id/sons/:id(.:format)      sons#update
                DELETE /fathers/:father_id/sons/:id(.:format)      sons#destroy
        fathers GET    /fathers(.:format)                          fathers#index
                POST   /fathers(.:format)                          fathers#create
     new_father GET    /fathers/new(.:format)                      fathers#new
    edit_father GET    /fathers/:id/edit(.:format)                 fathers#edit
         father GET    /fathers/:id(.:format)                      fathers#show
                PATCH  /fathers/:id(.:format)                      fathers#update
                PUT    /fathers/:id(.:format)                      fathers#update
                DELETE /fathers/:id(.:format)                      fathers#destroy

schema.rb

ActiveRecord::Schema.define(version: 20130626013724) do

  create_table "fathers",force: true do |t|
    t.string   "name"
    t.integer  "age"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "sons",force: true do |t|
    t.string   "name"
    t.integer  "age"
    t.integer  "father_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "sons",["father_id"],name: "index_sons_on_father_id"

end

问题是我应该为下面标有AA,BB,CC和DD的地方指定哪些路径和参数?
这是app / views / sons / index.html.erb文件的一部分:

<% @sons.each do |son| %>
      <tr>
        <td><%= son.name %></td>
        <td><%= son.age %></td>
         <td><%= link_to 'Show',AA %></td>
        <td><%= link_to 'Edit',BB(CC) %></td>
        <td><%= link_to 'Destroy',DD,method: :delete,data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>

我能够添加资源子.问题是指定它的显示和编辑路径.

代码也可用on github.

解决方法

阅读 Nested Rsources和 creating-nested-resources

<td><%= link_to 'Show',father_son_path(@father,son) %></td>
<td><%= link_to 'Edit',edit_father_son_path(@father,son) %></td>
<td><%= link_to 'Destroy',[@father,son],confirm: 'Are you sure?',method: :delete %></td>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读