Search code examples
ruby-on-railspathlink-to

Rails 4 - link_to helper - path to associated object


I am trying to make an app in Rails 4.

I'm still getting confused with basic concepts.

I'm trying to put a link on my profile show page, to the name of the associated organisations.

I have a profile model and an organisation model.

The associations are: profile belongs to organisation and organisation has_many profiles.

On my profile show page, I have tried every different way I can think of to try and make a link to the organisation show page for the associated organisation. I have tried:

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

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

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

I have tried about 20 variations on this. I don't know what I'm doing and I don't understand how to read the API docs. So I'm stuck.

Can anyone see what I've done wrong?


Solution

  • Try

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