Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specifying a correct path for Show and Edit actions for nested resources

I have 2 models

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 routes output:

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

The question is what paths and parameters should I specify for places marked AA, BB, CC and DD below? This is a part of app/views/sons/index.html.erb file:

 <% @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 %>

I'm able to add a resources son. The problem is to specify its show and edit pathes.

The code is also available on github.

like image 828
Askar Avatar asked Jun 26 '13 03:06

Askar


1 Answers

Read Nested Rsources and 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>
like image 173
rails_id Avatar answered Sep 27 '22 23:09

rails_id