我有一个显示 3 个搜索结果列表的页面:插图、小说和标签。我想让用户单击每个列表的链接以查看缩略图网格。我一生都无法弄清楚如何将搜索结果对象传递到新页面。我只得到插图ID的字符串。
main_controller 的搜索方法,以及我要对 show_illustrations 做的事情:
def search
@search_criteria = params[:search]
@novels = Novel.search(params[:search])
@illustrations = Illustration.search(params[:search])
@tags = Tag.search(params[:search])
respond_to do |format|
format.html #search_results.html.erb
end
end
def show_illustrations
@illustrations = params[:illustrations]
end
在搜索结果中显示插图列表的部分。即使我将 link_to 语句中的操作分配给“show_illustrations”,应用程序仍坚持调用 main 的 show 方法:
<% if @illustrations.size() > 0 %>
<div class="row-fluid">
<div class="span8">
<h3>Illustrations</h3>
</div>
<div class="span4">
<%= link_to "View", {:controller => "main", :action => "show_illustrations", :illustrations => @illustrations}, :class => "btn btn-mini"%>
</div></div>
<div class="search_results well-search-results">
<table class="table table-striped table-condensed">
<% @illustrations.each do |illustration| %>
<tr>
<td><%= illustration.name %></td>
<td><%= link_to "Show", illustration, :class => "btn btn-primary btn-mini", :style => "float:right;" %></td>
</tr>
<% end %>
</table>
</div>
<% else %>
<h3>No Matching Illustrations</h3>
<% end %>
应该显示插图网格的页面,标题为 searched_illustrations.html.erb:
<div class="container">
<div class="well padding:0px;">
<% @illustrations.each_slice(4) do |set| %>
<div class="row-fluid">
<% set.each do |illustration| %>
<div class="span3">
<div style="text-align:center"><%= link_to image_tag(illustration.image_thumbnail_url), illustration %>
<br />
<%= illustration.name %><br />
<%= link_to illustration.novel.name, illustration.novel %> -
<%= illustration.novel.edition %>
<% if ! illustration.tags.empty? %>
<br />
<% illustration.tags.each do |tag| %>
<%= tag %>
<% end %>
<% end %>
</div>
<br />
</div>
<% end %>
</div>
<br \>
<% end %>
</div>
</div>
路线.rb:
match 'main/show_illustrations' => 'main#show_illustrations', as: :show_illustrations