Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

ruby on rails - Stuck trying to list associated model

I think I am deeply misunderstanding how to write instances.

Miniatures have_many Manufacturers through the Productions table.

On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:

<% @miniature.manufacturers.each do |manufacturer| %>
    <%= link_to @miniature.manufacturer.name, manufacturer_path %>
<% end %>

Needless to say it does not work. It gives "undefined method `manufacturer'".

I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.

<% @miniature.manufacturers.each do |manufacturer| %>
    <%= link_to @miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>

I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.

If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Does this work:

<% @miniature.manufacturers.each do |manufacturer| %>
    <%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...