Rails link_to an .each join

a 夏天 提交于 2019-12-25 03:26:47

问题


I have this code which associates one table to another. Using Set it collects all data and only shows it once if there are other similar values.

genre_names = Set.new
<% @pm_relationships = PmRelationship.where(:people_id => @person.id) %>

<% @pm_relationships.each do |pm_relationship| %>
  <% @movie=Movie.find(pm_relationship.movie_id) %>
  <% @mg_relationships = MgRelationship.where(:movie_id => @movie.id) %>

  <% @mg_relationships.each do |mg_relationship| %>
    <% @genre=Genre.find(mg_relationship.genre_id) %>
    <% genre_names.add(@genre.name) %>    
  <% end %>
<% end%>

# actual view code
<ul class="basic-info-genres">
  <%= "<li>#{genre_names.to_a.join('</li><li>')}</li>".html_safe %>
</ul>

My problem here is how a link_to would work in the print code provided

<%= "<a><li><button><span>#{genre_names.to_a.join('</span></button></li></a><a><li><‌​‌​button><span>')}</span></button></li></a>".html_safe %>

How to make the above print to have this link to /genres/<%=@genre.id%>?

I've tried

<%= "<a href='/genres/#{@genre.id}'><li><button><span>#{genre_names.to_a.join('</span></‌​‌​button></li></a><a><li><button><span>')}</span></button></li></a>".html_safe %>

but this only links to the first genre shown

Any ideas?

Thanks


回答1:


Add to genres the whole @genres, not only the @genre-name, then you can use the each-loop in the view code.

For the Controller (you should have your programmic logic there):

 @genres = Set.new 
 @pm_relationships = PmRelationship.where(:people_id => @person.id) 
 @pm_relationships.each do |pm_relationship| 
   movie=Movie.find(pm_relationship.movie_id) 
   @mg_relationships = MgRelationship.where(:movie_id => movie.id)
   @mg_relationships.each do |mg_relationship| %>
     genre=Genre.find(mg_relationship.genre_id) %>
     @genres.add(genre) %>    
   end
 end

For the view:

 <ul class="basic-info-genres">
   <% @genres.each do |genre| %>
     <li><%= link_to genre.genre_name, "#{root_url}genres/#{genre.id}" %></li>
   <% end %>
 </ul>



回答2:


If I understand correctly, you want to link all genres that are associated with a movie.

To do this, in your controller, load the genres for a user's movies. With the correct association setup, this is as easy as @person.genres. So let's do that.

  class Person < ActiveRecord::Base
    has_many :pm_relationships
    has_many :movies, through: :pm_relationships
    has_many :genres, through: :movies
  end

  class PmRelationship < ActiveRecord::Base
    belongs_to :person
    belongs_to :movie
  end

  class Movie < ActiveRecord::Base
    has_many :mg_relationships
    has_many :genres, through: :mg_relationships
  end

With that setup, in your controller, just setup an instance variable to list all genres for @person

@genres = @person.genres

Then in your view, use the block form of link_to so it's easier to code your html

<ul class="basic-info-genres">
  <% @genres.each do |genre| %>
    <%= link_to genre do %>
      <li>
        <button>
          <span><%= genre.name %>‌</span>
        </button>
      </li>
    <% end %>
  <% end %>
</ul>



回答3:


Basically, you can't have any direct child element instead of li within ul. You will need to have <a> within li, however, we can apply css to <a> so that it looks like whole 'li' have the clickable link.

From your actual code, update the snippet.

<ul class="basic-info-genres">
  <% genre_names.each do |genre_name| %>
    <li>
      <%= link_to genre_name, genre_path(@genre), :class => 'clickable-li' %>
    </li>
  <% end %>
</ul>

You need to set your <a> to display: block;

# css file
.clickable-li {
  display: block;
}


来源:https://stackoverflow.com/questions/25157456/rails-link-to-an-each-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!