Azren
Azren

Reputation: 237

link to edit another model

I have a 2 models with the following association.

goal.rb

has_one :progress

progress.rb

belongs_to :goal

In the goal index page, I have a link that suppose to edit the progress record for that particular goal, but I couldn't get it to find the correct record id of the progress record. My link_to code is as per below. It will pass the goal id instead of the correct progress id.

app/view/goals/index.html.erb

<%= link_to 'Progress', edit_progress_path(goal) %>

How should I go about this.

Thank you.

Cheers, Azren

Upvotes: 0

Views: 4299

Answers (2)

jdoe
jdoe

Reputation: 15771

I'm fond of using Rails "magic":

<%= link_to 'Progress', [:edit, goal.progress] %>

Upvotes: 5

Mischa
Mischa

Reputation: 43298

You shouldn't pass a goal object, but a progress object to the edit_progress_path method:

<%= link_to 'Progress', edit_progress_path(goal.progress) %>

Upvotes: 2

Related Questions