gpt4 book ai didi

ruby-on-rails - 在 View 中显示自定义查询结果

转载 作者:数据小太阳 更新时间:2023-10-29 09:02:18 25 4
gpt4 key购买 nike

我对 Rails 比较陌生,除了在我的索引页面上显示“所有”项目外,我还想显示一个过滤列表。本质上是与另一个模型关联的记录子集。

我的模型是 Issue 和 Split。一个 Split 属于一个 issue,一个 issue 又属于多个 split。

如果 Split 与 issue_id = 1 关联,那么我希望它显示在该问题的拆分列表中

这是我的 Controller 代码:

class SplitsController < ApplicationController
before_action :set_split, only: [:show, :edit, :update, :destroy]

# GET /splits
# GET /splits.json
def index
@splits = Split.all
@chosen_splits = Issue.find(1)
end

这是我在索引 View 中的代码:

<tbody>
<% @chosen_splits.each do |split| %>

<tr>
<td><input type="checkbox" value></td>
<td><%= link_to split.name, split_path(split) %></td>
<td class="text-right"><%= number_with_delimiter(split.quantity) %></td>
<td><%= link_to 'Edit', edit_split_path(split), :class => 'btn btn-xs btn-default' %></td>
<td><%= link_to 'Delete', split, method: :delete, data: { confirm: 'Are you sure?'}, :class => 'btn btn-xs btn-danger' %></td>
</tr>
<% end %>
</tbody>

这是我的问题模型:

class Issue < ActiveRecord::Base

belongs_to :publication
has_and_belongs_to_many :splits
has_many :issue_split_geographies
belongs_to :medium

validates :name, :start_date, :status, presence: true

end

当我加载页面时,出现“未定义的方法 ‘each’ for”错误。但是,当我运行 @splits 变量时,同样的基本逻辑也在起作用。

非常感谢所有帮助。

最佳答案

你需要

@chosen_splits = Issue.find(1).splits

否则你会为你的问题调用.each方法

否则请在您的问题模型中将has_and_belongs_to_many :splits编辑为has_many :splits

关于ruby-on-rails - 在 View 中显示自定义查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32463551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com