dimanche 27 septembre 2015

Simple_form batchupdate associated model with checkboxes

I have a HABTM relation between (let's say) Posts and Tags. User can create some tags and assign them to some posts.

Now I am busy with a batch edit form. I would like to show all posts in the first column of a table and the second column the tags are shown.

    = simple_form_for(@posts, url: update_multiple_posts_path, method: :put) do |f|
      %table
        %th Post
        %th Tags
      %tbody
        - @posts.each do |post|
          =simple_fields_for 'posts[]', post do |post_form|
            %tr
              %td= post.title
              %td
                =post_form.association :tags
      =f.button :submit

So far so good. It does render the tags in as a dropdown and I can assign them to the post. After submit I get the following parameters:

"posts"=>{"10001"=>{"tag_ids"=>["", "1"]}, "10002"=>{"tag_ids"=>["", ""]}}, "commit"=>"Update"}

But instead of a dropdown, I would like to have the tags shown as checkboxes. So according the simple_form methode I just can add:

    =post_form.association :tags, as: :checkboxes

But when I do that, the form doesn't work anymore. It does show the tags as check_boxes and it also shows the ones that are active. But when I change a checkbox and submit it, I get the following error:

Couldn't find Post with 'id'=tag_ids

Now I get the following parameters:

"posts"=>{"tag_ids"=>["1", "", ""]}, "commit"=>"Update"}

Somehow the form does not parse the post_ids anymore.

Does anyone knows what I do wrong and what I should change? Thanks!




Aucun commentaire:

Enregistrer un commentaire