Mel
Mel

Reputation: 2715

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?

Upvotes: 0

Views: 214

Answers (2)

neanderslob
neanderslob

Reputation: 2703

Controller aside, I think I see what's going on. Try making the following edits

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

Unless you somehow set the local variable organisation elsewhere, you need to use your instance variable to specify the associated organisation, just like you did in naming the link.

Upvotes: 0

Matt
Matt

Reputation: 123

Try

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

Upvotes: 2

Related Questions