0

I have a profile resources with edit_profile_path

now I am trying add the link of it on a sitewide menu hence adding the link on application.html.erb

<%= link_to 'My Profile', edit_profile_path %>

All this looks very straight forward but I am getting error

No route matches {:action=>"edit", :controller=>"profiles"}

I did some searching and found this Rails 3 - Nested Resources Routing - One to One relationship but here the problem was nested routing which I don't have, here is what my rake routes give

edit_profile GET    /profiles/:id/edit(.:format)    profiles#edit

I am using devise but not sure this has anything to do with it though I do have

@profile = current_user.build_profile(params[:profile])

in profiles.controller.rb

Any idea how to show the profile links on application.html.erb ? I am sure it is something very small which I am missing out

EDIT - I have added (@profile) in link_to path, now I am getting this error -

No route matches {:action=>"edit", :controller=>"profiles", :id=>nil} ?

I am using devise and is using @profile = current_user.build_profile(params[:profile]) in profiles_controller.rb

Community
  • 1
  • 1
iCyborg
  • 4,699
  • 15
  • 53
  • 84

3 Answers3

1

When your profile model *belongs_to* the user one, the creation of a profile object requires a reference to which user object to associate it with. Therefore, you need to pass both the current_user and the @profile.

edit_profile_path(current_user, @profile)

PericlesTheo
  • 2,429
  • 2
  • 19
  • 31
  • Hi, now I am getting this error - No route matches {:action=>"edit", :controller=>"profiles", :id=>nil} ? I am using devise and is using @profile = current_user.build_profile(params[:profile]) in profiles_controller.rb – iCyborg Jun 18 '13 at 18:18
  • unfortunately @profile = current_user.profile.build(params[:profile]) is also not working – iCyborg Jun 18 '13 at 18:29
  • Thanks, this code worked edit_profile_path(current_user, @profile), can you tell me why we need current_user as an argument too ? – iCyborg Jun 18 '13 at 18:33
  • for sanity check, are you sure it works? Because I think it should be `edit_user_profile_path [current_user, @profile]` – PericlesTheo Jun 18 '13 at 18:40
  • [] is giving me error - syntax error, unexpected tLBRACK, expecting keyword_do or '{' or '(', I am using Rails 3.2.8, but () is working fine. EDIT - OK, this is the best working code <%= link_to 'Profile', edit_profile_path(current_user) %> – iCyborg Jun 18 '13 at 18:46
  • Do you have an association `has_one :user` and `has_one :profile`? – PericlesTheo Jun 18 '13 at 18:48
  • Then I don't know how come `edit_profile_path(current_user)` works. The correct route should be `edit_user_profile_path(current_user, @profile)`. – PericlesTheo Jun 18 '13 at 19:12
  • sorry I meant I have belongs_to :user (in profile model) and has_one :profile, :dependent => :destroy (in user model) – iCyborg Jun 18 '13 at 19:15
  • You don't have nested resources have you? – PericlesTheo Jun 18 '13 at 19:17
  • if ur profiles resource is nested inside user resources then u need to pass user object for whom the profile is to be updated. current_user should not be passed since u have specified that profiles resource is not nested. route has nothing to do with model relationship. if my profile model belongs to 10 different models, then considering the above, my route would be messed up. – Prasad Surase Jun 18 '13 at 19:38
0

Maybe you need to pass edit_profile_path a param...

<%= link_to 'My Profile', edit_profile_path(@profile) %>
a-b-r-o-w-n
  • 515
  • 2
  • 17
  • Hi, now I am getting this error - No route matches {:action=>"edit", :controller=>"profiles", :id=>nil} ? I am using devise and is using @profile = current_user.build_profile(params[:profile]) in profiles_controller.rb – iCyborg Jun 18 '13 at 18:17
  • I'm not sure exactly what `build_profile` does. If your User class has a profile, why not `@profile = current_user.profile`? My guess is that the `@profile` you are using in your view is not a `class Profile` instance, therefore doesn't have an id. Hard to say without seeing more of your code. – a-b-r-o-w-n Jun 20 '13 at 04:05
0

u need to provide a object that exists in the database. building doesnt mean that the object has been saved. hence its passing id as nil.

edit_profile_path(@profile) #@profile needs to exist in db. 
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58