ruby - RoR collection_select(不起作用) V collection_check_boxes(起作用!) - 为什么?
问题描述
我正在尝试使用 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 正在传回一个字符串/整数,而不是您的强参数所要求的数组。
当您将 multiple: true 添加到您的 collection_select 调用时会发生什么?
我猜您需要的是 Listing 模型中的 belongs_to :category 关系:
class Listing < ApplicationRecord
belongs_to :category
...
end
推荐阅读
- gremlin - Tinkerpop Gremlin - 如何简化 2 计数数组上的 {a - b} 数学?
- scala - Scala 模拟 when/thenReturn Future null
- python - 如何为流程挖掘准备一个包含 2 个日期时间列的 CSV 文件
- domain-driven-design - 仅为了降低投影逻辑的复杂性而创建领域事件
- c# - C# WPF 列在 ListView 内的对齐方式
- networking - 网络中点到点的距离,任意逻辑
- google-chrome - 浏览器如何使用电脑上配置的dns
- r - 错误:loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) 中的“EGSEA”的包或命名空间加载失败:
- python - 如何在python中使用pandas read_csv自动跳过具有非浮动值的行?
- c# - 多任务,如何同时在foreach中运行多个对象?