我想要多个(一个数组.)如果选择了多个列表,则将类别_id保存到每个列表中。下面是如何设置所有内容,包括类别如何与列表一起工作。
类别模式:
class Category < ApplicationRecord
validates_uniqueness_of :category
has_many :listings清单模式:
has_and_belongs_to_many :categories, required: false
attr_accessor :new_category_name
before_save :create_category_from_name
# has_many :categories计划(适用于类别和清单):
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "listings", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
t.integer "user_id"
t.integer "category_id"
t.index ["category_id"], name: "index_listings_on_category_id"
end然后,我在seed.rb中定义了类别,然后在需要添加它们时,使用rails db:seed来输入它们。
新上市主计长:
def new
@listing = Listing.new
@categories = Category.all
3.times do
@listing.categories.build
end
end创建列表的表单视图(简单地说):
<%= form_with(model: listing, local: true) do |form| %>
<%= form.label "Choose Up to 3 Categories (1 required)"%>
<div class="space">
<%= form.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category" %>
<%= form.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category" %>
<%= form.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category" %>
</div>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%= form.label "Choose Up to 3 Categories (1 required)"%>
<div class="space">
<% form.fields_for :category_id do |c| %>
<%= c.select :category_id, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category" %>
<% end %>
</div>
<% form.fields_for :category_id do |c| %>
<%= c.text_field :category_id %>
<% end %>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~在"~“之间是我试着测试它的。他们甚至没有出现在形式,但我不知道为什么。
当我使用第一个"form.select :category_id“时,它会出现3次下拉式下降,只有最后一次选择的下拉保存。如果我选择三个单独的类别,只有最后一个选择将保存。我希望能够为每个列表选择多个类别。
如何允许在创建新列表时保存多个类别?无论下拉列表、复选框等等,如果用户选择了多个列表,则只需要一个以上的列表就可以保存。
更新:
模式:
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categories_listings", id: false, force: :cascade do |t|
t.integer "category_id", null: false
t.integer "listing_id", null: false
end视图表格:
<%= form.select :category_ids, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category", :multiple => true %>控制对撞机:
params.require(:listing).permit(:attr1, :name, :description, :price, :image, :category_id, category_ids: [])型号:
Category
has_and_belongs_to_many :listings
Listing
belongs_to :category, required: false
belongs_to :categories_listings, required: false发布于 2018-10-18 03:06:04
当我使用第一个"form.select :category_id“时,它会出现3次下拉式下降,只有最后一次选择的下拉保存。如果我选择三个单独的类别,只有最后一个选择将保存。我希望能够为每个列表选择多个类别。
当然,您有三个相同的下拉列表,用于相同的属性,它只会选择最后一个下拉列表的选定值并将其传递给params。
您需要将其设置为one下拉,并允许在其上进行多次选择,如下所示
<%= form.select :category_ids, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select a Category", :multiple => true %>注意:category_ids和:multiple => true的更改。:category_ids是一种方法(collection_singular_ids),它为Rails下的Rails提供多到多个关联,以为已连接的表创建条目。
:multiple => true允许您选择多个选项并将这些ids作为数组传递。例如,像这样的category_ids: ["1","2","3"]
现在来看一下控制器代码,您的不应该为类别构建列表,因为您的方法是不同的。将新方法更改为下面
def new
@listing = Listing.new
end在@categories = Category.all中显式调用Category.all也没有必要使用select
最后,白名单category_ids在listing_params中,如下所示
params.require(:listing).permit(:attr1, .... category_ids: [])在"~“之间是我试着测试它的。他们甚至没有出现在形式,但我不知道为什么。
好吧,你在搞乱accepts_nested_attributes_for和fields_for。继续读!
注:
同样,正如@IIya 所提到的,您的模式对于HABTM关联来说是不同的。您应该修改您的模式以适应您的工作方法的需要。
更新#1
我使用类别作为搜索列表的一种方式。因此,在创建列表时,我希望能够选择多个类别来与列表一起进行。类别将被预先定义。
上面提到的方法应该适合您的需要,但是需要在代码中进行更多的调整才能使其工作。
1) Category模型中的关联存在缺陷。您需要将has_many :listings更改为has_and_belongs_to_many :listings
2)从类别模型中删除validates_uniqueness_of :category,因为它是无效的。
3)从category_id表中删除listings
4)为listings和categories生成一个HABTM连接表,如下所示
rails generate migration CreateJoinTableCategoryListing category listing现在,保存清单后,隐藏的rails将为categories_listings创建条目,这对于上述方法是正确的。
虽然,新的目录将/应该能够由管理员(而不是用户)添加,以便能够将该类别添加到现有的或新的列表中。
这也可以通过与上面相同的方法来创建一个新的列表和现有的类别。并仅呈现列表的编辑表单,以添加现有列表的现有类别。您只需要确保这些路由是不同的,并且只能由管理员访问。
更新#2:
您需要删除Listing模型中的当前关联,并在其中添加以下代码。
#listing.rb
has_and_belongs_to_many :categories并从params列表中删除category_id。只需保留category_ids: []
params.require(:listing).permit(:attr1, :name, :description, :price, :image, category_ids: [])发布于 2018-10-18 02:57:08
您的数据关系似乎不正确。看一看:
Category模型验证了category的唯一性。但在“类别”表中没有字段类别。似乎应该用名字代替。Listing模型has and belongs to many categories.对于Rails,这意味着这两个表通过第三个表categories_listings连接,后者包含category_id和listing_id。但是您的数据库模式是不同的--在您的表中,category_id属于listings表,它匹配Category模型has many listings关联。有个矛盾。我认为数据模型是整个应用程序的基础。拥有正确的数据模型,为控制器和视图编写代码变得透明和明显。首先修复您的数据模型,并使用嵌套属性保存关联模型。
发布于 2018-10-18 07:52:23
许多只支持嵌套参数数组,您应该允许像这个params.require(:listing).permit(:attr, ..., category_ids: [])这样的参数,然后用这段代码更改表单。
<%= form.select :category_ids, options_from_collection_for_select(Category.all, :id, :name), :prompt => "Select Categories", multiple: true %>https://stackoverflow.com/questions/52865065
复制相似问题