Saving collection_check_boxes state after form submit error in Rails

I am trying to save the state collection_check_boxes

after a failed check, my situation is:

Association :

post

has_many categories

throughpost_categories

View

<%= simple_form_for @post do |post| %>
  <%= post.collection_check_boxes :category, Category.all, :id, :category_name %>
  ---some other items---
  <%= post.button :submit, 'Create Post' %>
<% end %>

      

posts_controller

if @post.save
  flash[:notice] = "Post was created."
  redirect_to posts_path
else
  @post = Post.new(return_post_params)
  render :new
end

      

I put all the parameters in return_post_params

, the format category

is equal ["1", "2", "3"]

, but I don't know how to make the checkboxes checked after the check is complete.

Any ideas are appreciated!

+3


source to share





All Articles