【发布时间】:2015-09-03 00:54:13
【问题描述】:
无法理解如何在 rails 视图中使用 links_to 过滤同一控制器中的内容。我的代码如下:
#index.html.erb(链接导航区)
<nav>
<%= link_to 'Daily Monitoring', root_path(:category => "dailymonitoring") %>
<%= link_to 'Smoke Tests', root_path(:category => "smoketests") %>
</nav>
# index.html.erb(续)
<ul id="results">
<% if @reportlinks == "dailymonitoring" %>
# dailymonitoring content
<% elsif @reportlinks == "smoketests" %>
# smoketests content
<% end %> <!-- end conditional -->
</ul>
#reports_controller.rb
if params[:category]
@reportlinks = Report.where(:category => params[:category])
else
@reportlinks = Report.all
end
#模型(report.rb)
class Report
include ActiveModel::Model
attr_accessor :reports, :smokereports
belongs_to :reports, :smokereports, :reportlinks
end
我得到的错误是未定义方法 `belongs_to' for Report:Class 和 错误。不涉及数据库。我只是想让人们知道我想点击任何链接,并且只过滤该 if/else 语句中的内容块。
是否需要为 if/else 语句创建一个新的控制器才能工作?请让我知道是否需要更多代码才能更好地理解。谢谢。
【问题讨论】:
标签: ruby-on-rails ruby hyperlink model controller