RoR collection_select(不起作用)V collection_check_boxes(起作用!)——为什么?
RoR collection_select (does not work) V collection_check_boxes (works!) - Why?
我正在尝试使用 collection_select 将一些数据保存到我的模型中,但这不起作用,我不明白为什么。我尝试使用 collection_check_boxes ,它很容易工作。我使用的是 HABTM 方法,我确信我的 model.rb 文件编码正确,所以我认为下面的视图代码中的某些内容不正确。
我能得到一些帮助吗
----以下不工作
<div class="form-group control col-md-12 mb-4">
<%= form.label :category, 'Pick A Category Most Appropriate' %>
<div class= 'is-focused field has-addons control is-expanded select is-fullwidth'>
<%= form.collection_select :category_ids , Category.all, :id, :name %>
</div>
</div>
----以下工作正常!
<div class="field">
<%= form.label "Pick A Category Most Appropriate" %><br />
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.label %>
<%= b.check_box %>
</div>
<% end %>
</div>
如下所示,我得到了一个不允许的错误,但在使用 collection_check_boxes
时却没有
Started PATCH "/listings/1" for ::1 at 2020-01-07 20:33:13 +0000
Processing by ListingsController#update as HTML
Parameters: {"authenticity_token"=>"Y/rvJ3EcwV6iioSV8fXUkkwxRRiZ7o8ZV94sGS5G3liYnghcef2AWBc50g5gX4ULhmGyp163vtVfRAgil8gnMA==", "listing"=>{"name"=>"New Listing", "description"=>"This is a new listing", "category_ids"=>"1", "end_date(1i)"=>"2020", "end_date(2i)"=>"1", "end_date(3i)"=>"6", "end_date(4i)"=>"22", "end_date(5i)"=>"57"}, "commit"=>"Update Listing", "id"=>"1"}
Listing Load (4.8ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/listings_controller.rb:69:in `set_listing'
Unpermitted parameter: :category_ids
Redirected to http://localhost:3000/listings/1
Completed 302 Found in 30ms (ActiveRecord: 4.8ms | Allocations: 1492)
Started GET "/listings/1" for ::1 at 2020-01-07 20:33:13 +0000
Processing by ListingsController#show as HTML
Parameters: {"id"=>"1"}
Listing Load (0.6ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/listings_controller.rb:69:in `set_listing'
Rendering listings/show.html.erb within layouts/application
Rendered listings/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 313)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 50ms (Views: 44.4ms | ActiveRecord: 0.6ms | Allocations: 5953)
来自控制器的更新操作
def update
respond_to do |format|
if @listing.update(listing_params)
format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: @listing }
else
format.html { render :edit }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
强参数
def listing_params
params.require(:listing).permit(:name, :description, :end_date, category_ids:[])
end
来自上面的评论:
你的 collection_select 正在传回一个 string/integer 而不是你的强参数所需的数组。
将 multiple: true 添加到 collection_select 调用时会发生什么?
我猜您需要的是 belongs_to :Listing 模型中的类别关系:
class Listing < ApplicationRecord
belongs_to :category
...
end
我正在尝试使用 collection_select 将一些数据保存到我的模型中,但这不起作用,我不明白为什么。我尝试使用 collection_check_boxes ,它很容易工作。我使用的是 HABTM 方法,我确信我的 model.rb 文件编码正确,所以我认为下面的视图代码中的某些内容不正确。
我能得到一些帮助吗
----以下不工作
<div class="form-group control col-md-12 mb-4">
<%= form.label :category, 'Pick A Category Most Appropriate' %>
<div class= 'is-focused field has-addons control is-expanded select is-fullwidth'>
<%= form.collection_select :category_ids , Category.all, :id, :name %>
</div>
</div>
----以下工作正常!
<div class="field">
<%= form.label "Pick A Category Most Appropriate" %><br />
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.label %>
<%= b.check_box %>
</div>
<% end %>
</div>
如下所示,我得到了一个不允许的错误,但在使用 collection_check_boxes
时却没有Started PATCH "/listings/1" for ::1 at 2020-01-07 20:33:13 +0000
Processing by ListingsController#update as HTML
Parameters: {"authenticity_token"=>"Y/rvJ3EcwV6iioSV8fXUkkwxRRiZ7o8ZV94sGS5G3liYnghcef2AWBc50g5gX4ULhmGyp163vtVfRAgil8gnMA==", "listing"=>{"name"=>"New Listing", "description"=>"This is a new listing", "category_ids"=>"1", "end_date(1i)"=>"2020", "end_date(2i)"=>"1", "end_date(3i)"=>"6", "end_date(4i)"=>"22", "end_date(5i)"=>"57"}, "commit"=>"Update Listing", "id"=>"1"}
Listing Load (4.8ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/listings_controller.rb:69:in `set_listing'
Unpermitted parameter: :category_ids
Redirected to http://localhost:3000/listings/1
Completed 302 Found in 30ms (ActiveRecord: 4.8ms | Allocations: 1492)
Started GET "/listings/1" for ::1 at 2020-01-07 20:33:13 +0000
Processing by ListingsController#show as HTML
Parameters: {"id"=>"1"}
Listing Load (0.6ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/listings_controller.rb:69:in `set_listing'
Rendering listings/show.html.erb within layouts/application
Rendered listings/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 313)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 50ms (Views: 44.4ms | ActiveRecord: 0.6ms | Allocations: 5953)
来自控制器的更新操作
def update
respond_to do |format|
if @listing.update(listing_params)
format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: @listing }
else
format.html { render :edit }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
强参数
def listing_params
params.require(:listing).permit(:name, :description, :end_date, category_ids:[])
end
来自上面的评论:
你的 collection_select 正在传回一个 string/integer 而不是你的强参数所需的数组。
将 multiple: true 添加到 collection_select 调用时会发生什么?
我猜您需要的是 belongs_to :Listing 模型中的类别关系:
class Listing < ApplicationRecord
belongs_to :category
...
end