Rails 同一控制器中的类别(或过滤器)链接?

Rails category (or filter) links in same controller?

无法理解如何在 rails 视图的同一控制器中使用 links_to 过滤器内容。我的代码如下:

# index.html.erb(link 导航区域)

<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

我得到的错误是 undefined method `belongs_to' for Report:Class and a 错误。不涉及数据库。我只是想让大家知道我想单击任何 link 并且只过滤该 if/else 语句中的内容块。

我需要为 if/else 语句创建一个新的控制器吗?如果需要更多代码来更好地理解,请告诉我。谢谢。

belongs_toActiveRecord::Associations 中定义,它是 ActiveRecord 的一部分。您手动包含 ActiveModel::Model,它不提供任何与关联相关的功能。

Includes the required interface for an object to interact with ActionPack, using different ActiveModel modules. It includes model name introspections, conversions, translations and validations. Besides that, it allows you to initialize the object with a hash of attributes, pretty much like ActiveRecord does.

假设您不需要整个 ActiveRecord 数据库持久性功能,但需要关联样式,那么您需要手动处理。

因此,您必须为 append/remove 关联记录定义自己的方法并跟踪它们。

ActiveRecord 的关联特性与数据库持久化紧密相关。