【问题标题】:Rails category (or filter) links in same controller?同一控制器中的 Rails 类别(或过滤器)链接?
【发布时间】: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


    【解决方案1】:

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

    包括对象与 ActionPack 交互所需的接口,使用不同的 ActiveModel 模块。它包括模型名称自省、转换、翻译和验证。除此之外,它还允许您使用属性哈希初始化对象,就像 ActiveRecord 所做的那样。

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

    因此,您必须定义自己的方法来附加/删除相关记录并跟踪它们。

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

    【讨论】:

      猜你喜欢
      • 2013-07-21
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多