1

I'm trying to figure out nested routes.

I have models called Project and Potential Use. The associations are:

Project

has_many :potential_uses
      accepts_nested_attributes_for :potential_uses, reject_if: :all_blank, allow_destroy: true

Potential Use

belongs_to :project
belongs_to :user

In my projects show, I'm trying to loop through each potential use.

<% @project.potential_uses.each do |pu| %>
      <div class="row">
            <p><%= pu.comment %></p>
            <strong><%= pu.user.formal_name %></strong>
              <%= link_to 'Edit', edit_project_potential_use_path(pu) %>

            <p style= 'border-bottom: solid; border-bottom-width: 1px'></p> 

      </div>    
    <% end %>

When i try this edit link, I get an error that says:

No route matches {:action=>"edit", :controller=>"potential_uses", :id=>nil, :project_id=>#<PotentialUse id: 14, comment: "asdf", project_id: 17, created_at: "2016-08-19 01:36:49", updated_at: "2016-08-19 01:36:49", user_id: 1, private_comment: false>} missing required keys: [:id]

I've tried several different versions of the edit link.

<%= link_to 'Edit', edit_project_potential_use_path(potential_use) %>


<%= link_to 'Edit', edit_project_potential_use_path(project.potential_use) %>

I can't find a way that works.

My rake routes, shows this for the edit action:

edit_project_potential_use GET       /projects/:project_id/potential_uses/:id/edit(.:format)     potential_uses#edit

To my eye, this looks like I need to ask for plural references to the resources in the edit link (although it doesnt make sense to me), like so:

<%= link_to 'Edit', edit_projects_potential_uses_path(pu) %>

That formulation gives this error:

undefined method `edit_projects_potential_uses_path' for #<#<Class:0x007fd4b7431b18>:0x007fd4b7430678>
Did you mean?  edit_project_potential_use_path
               edit_project_potential_use_url

I can't see how to take this information and use it to write an edit link.

Can anyone see what I've done wrong?

Mel
  • 2,481
  • 26
  • 113
  • 273

2 Answers2

3

You have to specify a parent too:

edit_project_potential_use_path(@project, pu)
dskecse
  • 457
  • 6
  • 16
1

According to your rake routes, I think it the path should be edit_project_potential_use_path(@project, pu)

<%= link_to 'Edit', edit_project_potential_use_path(@project, pu) %>
Tan Nguyen
  • 3,281
  • 1
  • 18
  • 18