-3

I'm trying to make an app in Rails 4.

I have a profile model, and an organisation model. Organisation has an attribute called 'title'.

Associations are:

Profile belongs_to organisation 

Organisations has_many profiles

I'm trying to figure out how to write a link from my profile show page to the organisation show page.

I feel that this is correct:

<%= link_to @profile.organisation.try(:title).upcase, organisation_path(@profile.organisation.id) %>

However, when I try it, I get this error:

undefined method `id' for nil:NilClass

I can see in my console that the profile.organisation id is not nil - it has a value.

Can anyone see what I've done wrong?

rake routes - for organisation:

organisations GET       /organisations(.:format)                                    organisations#index
                            POST      /organisations(.:format)                                    organisations#create
           new_organisation GET       /organisations/new(.:format)                                organisations#new
          edit_organisation GET       /organisations/:id/edit(.:format)                           organisations#edit
               organisation GET       /organisations/:id(.:format)                                organisations#show
                            PATCH     /organisations/:id(.:format)                                organisations#update
                            PUT       /organisations/:id(.:format)                                organisations#update
                            DELETE    /organisations/:id(.:format)                                organisations#destroy
Mel
  • 2,481
  • 26
  • 113
  • 273
  • try this `@profile.organisation` instead of this `@profile.organisation.id` – uzaif Apr 15 '16 at 04:53
  • If I delete id from the end of that line, I get this error: No route matches {:action=>"show", :controller=>"organisations", :id=>nil} missing required keys: [:id] – Mel Apr 15 '16 at 04:55
  • show me your `rake routes` and model association .. – uzaif Apr 15 '16 at 04:56
  • updated to add them above. – Mel Apr 15 '16 at 05:04
  • That particular `profile` has no `organisation` associated to it, so is the error. – Pavan Apr 15 '16 at 05:06
  • what is output of `@profile.organisation` code in your console – uzaif Apr 15 '16 at 05:10
  • try this `organisation_path(@profile)` – uzaif Apr 15 '16 at 05:11
  • @user2860931, did u try `@profile.organisation_id` ? since profile is belongs, u have to have `organisation_id` association. that should work. – 7urkm3n Apr 15 '16 at 05:33
  • @7yrjn3n When I try that, I get an error that says: Couldn't find Organisation with 'id'=16. The organisation_id in this case is '1' (the profile id is 16) – Mel Apr 15 '16 at 05:35
  • @uzaif - no that doesnt work either – Mel Apr 15 '16 at 05:36
  • @user2860931, do u have `organisation_id` in your `Profiles` table ? – 7urkm3n Apr 15 '16 at 05:40
  • Yes. and I can see from my console that it is populated with id: 1 – Mel Apr 15 '16 at 05:40
  • @user2860931 can u test it in `rails console`. `o = Organisation.first` then `o.profiles` if you are able to see the profiles. then make sure in a browser that your current_user had been associated with organisation. – 7urkm3n Apr 15 '16 at 05:44
  • @7urkm3n - no that's not right. Profile belongs to Organisation. The Profile model has an organisation id. I can see in the console that the profile has been updated with an organisation id (with number '1'). What I can't figure out is how to display the name of the associated organisation on the profile show page – Mel Apr 15 '16 at 06:09
  • @user2860931 Basically, yr saying associated and IDs correctly passed and having trouble to pull data from `@profile.organisation.id` and `@profile.organisation_id` both didnt work ? then u definitely have an issue in ASSOCIATION !!! – 7urkm3n Apr 15 '16 at 06:16

1 Answers1

0

Actually -the way I had the code was right. I had to reset my db and start over. Something strange caused the db connection to go awry. If others are looking for links, the way I did it originally was right.

Mel
  • 2,481
  • 26
  • 113
  • 273