本文介绍了将复选框数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我陷入了困境,尽管我的数组参数被捕获,但无法将其插入到数据库中.我没有收到不允许的参数错误或任何东西.插入到数据库时,它只是无法识别数组.
我想做什么:捕获任何被选中的框,并将数据作为单独行插入到数据库中.
这是我所拥有的:
/subscribe/categories/2
<%= simple_form_for @subscription do |f|%><div class="form-inputs"><%= f.hidden_field :dashboard_id, value: 1%><%= f.hidden_field :category_id,值:@category.id %><%= f.collection_check_boxes :feed_id, Feed.where("category_id = ?", @category), :id, :name %>
<div class="form-actions"><%= f.button :submit %>
<%结束%>
类别控制器
def show@subscription = Subscription.new结尾
订阅控制器
def subscription_paramsparams.require(:subscription).permit(:dashboard_id, :category_id, :feed_id => [])结尾
提交后,控制台输出如下:
由 SubscriptionsController#create as HTML 处理参数:{"utf8"=>"✓", "authenticity_token"=>"Zw2VkwujDLQjV4krjPF8N1EiYo5L/XOrUwedlHCvwB0=", "subscription"=>{"dashboard_id"=>"1", "category;"2", "feed_id"=>["3", "4", ""]}, "commit"=>"创建订阅"}(0.2ms) 开始SQL (1.6ms) INSERT INTO `subscriptions` (`category_id`, `created_at`, `dashboard_id`, `updated_at`) VALUES (2, '2014-01-06 02:17:41', 1, '2014-01-06 02:17:41')(116.6ms) 提交重定向到 http://localhost:3000/subscriptions/3在 173ms 内完成 302 Found (ActiveRecord: 119.3ms)
两个问题:
- 为什么我的 feed_id 数组有一个额外的"?(只有 2 个可能的复选框)
- 为什么我不捕获数组以将其插入数据库?
谢谢!
解决方案
对于任何想知道如何做到这一点的人,这里是我想出的一个解决方案.我第一篇文章中的所有内容都保持不变.在我的 SubscriptionsController(从中创建表单)中,这是我的创建操作:
def 创建仪表板 = params[:subscription][:dashboard_id]category = params[:subscription][:category_id]feed = params[:subscription][:feed_id]@subscription = feed.map { |订阅|Subscription.create(dashboard_id:仪表板,category_id:类别,feed_id:订阅)}结尾
如宣传的那样工作.如果有人出于某种原因认为我忽略了这是一个糟糕的主意,请发表评论.
I am stuck in that although my array parameter is being captured, it fails to insert it into the database. I do not get an unpermitted parameters error or anything. It just fails to recognize the array when inserting to the DB.
What I would like to do: Capture any box that is checked off, and insert the data as separate rows into the database.
Here is what I have:
/subscribe/categories/2
<div>
<%= simple_form_for @subscription do |f| %>
<div class="form-inputs">
<%= f.hidden_field :dashboard_id, value: 1 %>
<%= f.hidden_field :category_id, value: @category.id %>
<%= f.collection_check_boxes :feed_id, Feed.where("category_id = ?", @category), :id, :name %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
CategoriesController
def show
@subscription = Subscription.new
end
SubscriptionsController
def subscription_params
params.require(:subscription).permit(:dashboard_id, :category_id, :feed_id => [])
end
When submitted, here is the console output:
Processing by SubscriptionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Zw2VkwujDLQjV4krjPF8N1EiYo5L/XOrUwedlHCvwB0=", "subscription"=>{"dashboard_id"=>"1", "category_id"=>"2", "feed_id"=>["3", "4", ""]}, "commit"=>"Create Subscription"}
(0.2ms) BEGIN
SQL (1.6ms) INSERT INTO `subscriptions` (`category_id`, `created_at`, `dashboard_id`, `updated_at`) VALUES (2, '2014-01-06 02:17:41', 1, '2014-01-06 02:17:41')
(116.6ms) COMMIT
Redirected to http://localhost:3000/subscriptions/3
Completed 302 Found in 173ms (ActiveRecord: 119.3ms)
Two questions:
- Why is there an extra "" for my feed_id array? (Only 2 possible checkboxes)
- Why am I not capturing the array to insert it into the database?
Thanks!
解决方案
For anyone that wants to know how to do this, here is one solution I've come up with. Everything in my first post remains the same. In my SubscriptionsController (from which the form is created), here is my create action:
def create
dashboard = params[:subscription][:dashboard_id]
category = params[:subscription][:category_id]
feed = params[:subscription][:feed_id]
@subscription = feed.map { |subscribe| Subscription.create(dashboard_id: dashboard, category_id: category, feed_id: subscribe) }
end
Works as advertised. If anyone thinks for some reason that I am overlooking this is a terrible idea, please comment.
这篇关于将复选框数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!