Rails: 如何创建带有条件链接的列表
Rails: how to create a list with conditional links
我正在开发一个 RAILS 应用程序,我在其中创建了一个视图,其中列出了给定模型的所有可用资源 species.rb
。
部分视图是:
<% i= @s
for species in @species %>
<%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %>
<% i -= 1
end %>
部分资源species
有相关文章,其他资源只有名称。我想遍历部分内容并仅向具有相关文章的条目添加 link。
类似的东西:如果存在 species.article
则添加 link 否则只需将 species.name
不带 link + 遍历所有条目。
我该怎么做?
更新:
多亏了@jvillian 和@fool-dev,我才得以进步。在我的例子中,如果资源在其 table.
的描述行中有描述,我想添加一个 link
<% @species.each do |species| %>
<div class="entry">
<p><i><%= link_to_if(species.txtlandscape.present?, "#{species.name}, #{species.author.surname}, #{species.author.initial_name}. 2014", :controller => 'projects', :action => 'show', :id => species) %></i></p>
</div>
<% end %>
现在添加了 link 我想知道它是否可以用于将部分加载到目标,例如 in,其中 ArticleRequest 是我拥有的 JS 函数:
<% @ species.each do | species | %>
<div id="species-<%= species.id %>" class="species-entry">
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %></p>
</a>
</div>
<% end %>
在我找到使用 link_to_if
的方法之前,我将使用类似的东西:
<% for species in @species %>
<% if species.txtlandscape.present? %>
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
</a>
<% else %>
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
<% end %>
<% end %>
根据docs,看来你应该可以做到:
<% @species.each do |specie| %>
<%= link_to_if(specie.article, specie.name, specie_article_path(specie.article)) %>
<% end %>
路径名是我编的,你必须让它与你的实际路线相匹配。
顺便说一句,这个:
for species in @species
超级non-idiomatic。
你可以这样做,见下文
<% for species in @species %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
您可以使用 Rails each
方法,如下所示
<% @species.each do |species| %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
或者你可以用link_to_if
也是最容易理解的
<% @species.each do |species| %>
<%= link_to_if(species.article.present?, "#{species.name},", link_path(species.article)) %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
希望对您有所帮助。
我正在开发一个 RAILS 应用程序,我在其中创建了一个视图,其中列出了给定模型的所有可用资源 species.rb
。
部分视图是:
<% i= @s
for species in @species %>
<%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %>
<% i -= 1
end %>
部分资源species
有相关文章,其他资源只有名称。我想遍历部分内容并仅向具有相关文章的条目添加 link。
类似的东西:如果存在 species.article
则添加 link 否则只需将 species.name
不带 link + 遍历所有条目。
我该怎么做?
更新:
多亏了@jvillian 和@fool-dev,我才得以进步。在我的例子中,如果资源在其 table.
的描述行中有描述,我想添加一个 link<% @species.each do |species| %>
<div class="entry">
<p><i><%= link_to_if(species.txtlandscape.present?, "#{species.name}, #{species.author.surname}, #{species.author.initial_name}. 2014", :controller => 'projects', :action => 'show', :id => species) %></i></p>
</div>
<% end %>
现在添加了 link 我想知道它是否可以用于将部分加载到目标,例如 in,其中 ArticleRequest 是我拥有的 JS 函数:
<% @ species.each do | species | %>
<div id="species-<%= species.id %>" class="species-entry">
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %></p>
</a>
</div>
<% end %>
在我找到使用 link_to_if
的方法之前,我将使用类似的东西:
<% for species in @species %>
<% if species.txtlandscape.present? %>
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
</a>
<% else %>
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
<% end %>
<% end %>
根据docs,看来你应该可以做到:
<% @species.each do |specie| %>
<%= link_to_if(specie.article, specie.name, specie_article_path(specie.article)) %>
<% end %>
路径名是我编的,你必须让它与你的实际路线相匹配。
顺便说一句,这个:
for species in @species
超级non-idiomatic。
你可以这样做,见下文
<% for species in @species %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
您可以使用 Rails each
方法,如下所示
<% @species.each do |species| %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
或者你可以用link_to_if
也是最容易理解的
<% @species.each do |species| %>
<%= link_to_if(species.article.present?, "#{species.name},", link_path(species.article)) %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
希望对您有所帮助。