Inserting checkbox array into database

谁说我不能喝 提交于 2021-02-05 07:57:05

问题


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:

  1. Why is there an extra "" for my feed_id array? (Only 2 possible checkboxes)
  2. Why am I not capturing the array to insert it into the database?

Thanks!


回答1:


The reason your array is not being inserted into the database is that Active Record currently does not support the Postgresql array type. In order to insert these as separate rows the check-boxes need to be represented as individual instances of a model.

Possibly something like...

Category < ActiveRecord::Base
    has_many: feeds
    ...
end  

Feed < ActiveRecord::Base  
    belongs_to: category
    ...
end  

Now this would also mean that you would need to use the form_tag helper instead of the form_for. This would allow you to create a composite form consisting of multiple individual objects. Inserting this would just mean iterating and inserting over each object; giving you separate rows. Hope this helps.




回答2:


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.



来源:https://stackoverflow.com/questions/20940262/inserting-checkbox-array-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!