Ruby on rails 在RubyonRails中创建对象列表或数组
我试图让我正在构建的web应用程序的用户能够创建他们创建的对象列表 例如,用户有一个对象列表,比如杂货,可以是从苹果到橙子再到馅饼的任何东西 然后,我希望能够返回用户添加到数据库中的所有杂货,并通过选择那些应该在他们的杂货清单上的杂货来创建一个清单 最好采用这样的样式,即他们可以单击所需复选框,然后单击“保存”以创建新列表 我已经研究了所属关系,并尝试创建一个包含许多杂货的列表对象,但我无法理解此策略的组成部分。如果您能给我提些建议,我将不胜感激。谢谢 这是我目前拥有的代码,我最初省略了它,因为我认为我没有走上正确的道路,但这里无论如何只是以防万一/提供更多上下文: 杂货店模式:Ruby on rails 在RubyonRails中创建对象列表或数组,ruby-on-rails,ruby,forms,Ruby On Rails,Ruby,Forms,我试图让我正在构建的web应用程序的用户能够创建他们创建的对象列表 例如,用户有一个对象列表,比如杂货,可以是从苹果到橙子再到馅饼的任何东西 然后,我希望能够返回用户添加到数据库中的所有杂货,并通过选择那些应该在他们的杂货清单上的杂货来创建一个清单 最好采用这样的样式,即他们可以单击所需复选框,然后单击“保存”以创建新列表 我已经研究了所属关系,并尝试创建一个包含许多杂货的列表对象,但我无法理解此策略的组成部分。如果您能给我提些建议,我将不胜感激。谢谢 这是我目前拥有的代码,我最初省略了它,因为
class Item < ApplicationRecord
belongs_to :list, optional: true
end
类项
列表模型
class List < ApplicationRecord
has_many :items
end
类列表
列表控制器
class ListsController < ApplicationController
before_action :authenticate_user!
layout 'backend'
def index
@lists = List.where(user_id: current_user.id)
end
def show
end
def new
@list = List.new
end
def edit
end
def create
@list = List.new(list_params)
@list.user = current_user
if @list.save
redirect_to list_path(@list.id), notice: 'List was successfully created.'
else
redirect_to list_path(@list.id), notice: 'List was not created.'
end
end
def update
respond_to do |format|
if @list.update(list_params)
format.html { redirect_to @list, notice: 'List was successfully updated.' }
format.json { render :show, status: :ok, location: @list }
else
format.html { render :edit }
format.json { render json: @list.errors, status: :unprocessable_entity }
end
end
end
def destroy
@list.destroy
respond_to do |format|
format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def list_params
params.require(:list).permit(:name, :items)
end
end
class ListsController
不确定如何处理表单-我尝试了一些类似的方法,我将通过实现第三个模型来解决这个问题,该模型保持杂货和列表之间的关联。然后,您可以使用
:接受
的_嵌套的_属性_以表单形式处理它
举个例子,我将如何构建模型:
class List < ApplicationRecord
has_many :list_items, inverse_of: :list
has_many :items, through: :list_items
# This allows ListItems to be created at the same time as the List,
# but will only create it if the :item_id attribute is present
accepts_nested_attributes_for :list_items, reject_if: proc { |attr| attr[:item_id].blank? }
end
class Item < ApplicationRecord
has_many :list_items
has_many :lists, through: :list_items
end
class ListItem < ApplicationRecord
belongs_to :list, inverse_of: :list_items
belongs_to :item
end
因此,在上面的表单中,我替换了这些默认值,以便在选中复选框时,它具有来自item.id
的值,如果未选中,则该值为空。将其与列表模型中的accepts\u nested\u attributes\u的声明相结合,在列表模型中,如果:item\u id
为空,则表示应该拒绝该声明,并且我们得到的结果是仅为选中的项创建ListItems
最后一件事是允许在控制器中嵌套属性,如下所示:
def allowed_params
params.require(:list).permit(list_items_attributes: [:item_id])
end
所以我找到了下面的问题,并用它来实现我想要的
我只是实现了以下内容
<% form_for @list do |f| %>
<% Item.all.each do |item|
<%= f.check_box(:items, { :multiple => true }, item.id) %>
<% end %>
<% end %>
在这一点上,我的错误是没有分配列表id。我进行了一次迁移,以便将AtterBute“list_id”添加到项目模型中,这修复了它,所有工作都按照我的要求进行
有什么问题吗?你的代码目前看起来像什么?在你的问题中添加一些代码,你到底选择了什么?请看看我做了什么-这对我来说似乎相当复杂。它看起来不错,只要它能工作:)我展示的方法可能有点复杂,是的,当你想添加更多的功能时,比如你想添加多少个项目,以及你在不同的列表中添加了多少次项目,这可能更合适,但如果你不想这样做,那么你的方法就可以了。最后一点,解决未分配:list_id的问题的另一种方法是从list的关联中创建项,即@Item=@list.items.create(允许的参数)
。默认情况下,将分配列表id
def allowed_params
params.require(:list).permit(list_items_attributes: [:item_id])
end
<% form_for @list do |f| %>
<% Item.all.each do |item|
<%= f.check_box(:items, { :multiple => true }, item.id) %>
<% end %>
<% end %>