ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Unable to access :user_id info for routes #821

Closed justwes2 closed 7 years ago

justwes2 commented 7 years ago

I'm getting this error: No route matches {:action=>"edit", :controller=>"goals", :id=>"1", :user_id=>nil} missing required keys: [:user_id]. The line in question is:

</tr>
  <% current_user.goals.each do |goal| %>
    <tr>
     ...
      <td><%= link_to 'Edit', edit_user_goal_path(@user, goal) %> </td>
     ...
    </tr>
  <% end %>

The edit on the controller is:

def edit
    @user = current_user
    @goal = Goal.find(params[:id])
  end

and my repo is linked here: https://github.com/justwes2/wellnyss I'm not sure what I'm missing to access the edit_user_goal here.

amaseda commented 7 years ago

Does this still happen if you place current_user directly inside of edit_user_goal_path?

justwes2 commented 7 years ago

No. That fixed it. Is there a way to alter all user_id instances across a session to always be current user? The structure of my app should not allow for any interaction between siloed user experiences.

amaseda commented 7 years ago

Interesting. Not sure why current_user worked there and @user didn't (aside from working on the wrong controller action).

I think using current_user wherever you would have written out @user is the DRY-est solution. You're going to have to pass a user into those path helpers no matter what, and the shortest way is to avoid defining it in a controller action altogether.

justwes2 commented 7 years ago

I don't want to open a new issue for this, since the documentation is the same, but now that I can edit stuff, the program is spitting out the original goal and the edited goal to my index printout. Do I need to call the put method explicitly somewhere? I checked the scribbler solution branch, and I don't see where my code is different.