4

I'm trying to figure out how to make an app with Rails 4. I keep getting stuck on basic things and I don't seem to be able to identify principles to use going forward.

I have a profile model and a industry model. The associations are:

Profile:

has_and_belongs_to_many :industries, join_table: 'industries_profiles'

Industry:

has_and_belongs_to_many :profiles, join_table: 'industries_profiles'

In my profile show page, I'm now trying to link to the industry page:

<% @profile.industries.limit(5).each do |industry| %>

    <%= link_to industry.sector.upcase, industry_path(@industry) %> 

<% end %>   

I can't find anything that works for this link.

I have tried the following:

industry_path(@profile.industry)
industry_path(@profile.industry_id)
industry_path(industry)
industry_path(profile.industry)
industry_path(industry.id)
industry_path(industry_id)

But all of them are guesses. I don't know how to ready the API dock so I can't understand any of its content.

Can anyone see how to link to a show page of the other side of the HABTM association for a single record?

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
Mel
  • 2,481
  • 26
  • 113
  • 273
  • What do you have your routes.rb file? `industry_path(industry)` is one you should be using. – Ben Hawker Apr 19 '16 at 03:01
  • I have: resources :industries – Mel Apr 19 '16 at 03:03
  • When I try that, when I hover over the link, it shows a path which has the right industry id in it. But I can't click on it - nothing happens – Mel Apr 19 '16 at 03:05
  • As Ben said, `industry_path(industry)` should work, what do you mean by 'nothing happens'? if the href has the id on it, then it's working OK – jandresrodriguez Apr 19 '16 at 03:16
  • Try this way: <%= link_to industry.sector.upcase, @industry %> – Alexei.B Apr 19 '16 at 03:23
  • There's not enough information to effectively answer this question. Can you post the section of your `routes.rb` that includes the routes for both the `profile` and `industry` resources? – Anthony E Apr 19 '16 at 03:52
  • Did you write a migration for the join table? – hypern Apr 19 '16 at 12:53
  • I did. Ben/Jan are right in the format of the link. I don't know what was causing the problem. I was trying to get it working in safari - I don't know whether there is an explanation but when I tried in chrome/firefox it worked. Then when I went back to safari, it also worked. I'm not sure whether there is a reason to explain this. – Mel Apr 19 '16 at 23:53

1 Answers1

0

You can grab a list of your routes by running rake routes | grep industry in your command line, which will give you a table with the prefix, action, and uri pattern. For example:

   industries GET    /industries(.:format)           industries#index
              POST   /industries(.:format)           industries#create
 new_industry GET    /industries/new(.:format)       industries#new
edit_industry GET    /industries/:id/edit(.:format)  industries#edit
     industry GET    /industries/:id(.:format)       industries#show
              PATCH  /industries/:id(.:format)       industries#update
              PUT    /industries/:id(.:format)       industries#update
              DELETE /industries/:id(.:format)       industries#destroy

In your case, you should look at the show path. Which is industry and you append _path to the end of whatever your prefix is above, which comes out to be industry_path. And since you have declared your variable industry when defining your loop, you can use that instead of the instance variable.

Short answer: industry_path(industry)

sclem72
  • 466
  • 6
  • 16