0

I'm trying to pass multiple checkboxes as array to a db table. So i want to add multiple topics to a project.

= form_for @project, url: admin_projects_path, :html => { :multipart => true } do |p|
  %p
    =p.label :name
    =p.text_field :name 
  %p
    = p.label :topic
  %p
    = p.label "Value1"
    = p.check_box(:topic, {:multiple => true}, "value1", nil)
    = p.label "Value2"
    = p.check_box(:topic, {:multiple => true}, "value2", nil)
    = p.label "Value3"
    = p.check_box(:topic, {:multiple => true}, "value3", nil)
  %p
    = p.submit

but here topic returns nil in the projects, even whenever i check multiple checkboxes (i added to the whitelist params in the controller, so that cant be the cause)

What am i doing wrong here?

EDIT:

This is the code of the controller:

def new
  @project = Project.new
end

def create
  @project = Project.new(project_params)

  if @project.save
    redirect_to admin_projects_path
  else
    render :new
  end
end

private
# param white listing

def project_params
  params.require(:project).permit(:name, :description, :photo, :project_type, :highlight, :content, :topic, :category)
end

Solution found:

View was correctly.

 serialize :topic

needed be added to the project model

and the permit params should be edited so they only accept arrays on topic

private
# param white listing

def project_params
  params[:project][:topic] ||= []
  params.require(:project).permit(:name, :description, :content, topic: [])
end
5
  • Possibly unrelated but you don't need to make the form multipart unless you plan on having a file upload field in there. I'd expect that to send through params[:project][:topic] = ["value1", "value2", "value3"]. Can you please make an edit to your question and add what is actually coming through in params when you check all the checkboxes and submit the form? Also, add to your question the controller code which uses those params. It's possible that your view code is not the problem. Commented Sep 4, 2015 at 11:44
  • I having a fileupload aswell in it. but removed that to keep it clean for stackoverflow. Even when i check all the checkboxes, it returns nil. Commented Sep 4, 2015 at 11:51
  • Add the params submitted by the form as well please. Commented Sep 4, 2015 at 12:02
  • ?? all the other params submit with no problem and correctly, only the checkboxes with array give nil back Commented Sep 4, 2015 at 12:07
  • Well, if you ADD THE PARAMS TO YOUR QUESTION SO THAT WE CAN SEE THEM SOMEONE MIGHT BE ABLE TO DIAGNOSE THE ISSUE. Sorry for shouting but i've asked you twice now and you seem to not be comprehending. Commented Sep 4, 2015 at 12:42

1 Answer 1

2

If you save array in database add this to your model:

class Project < ActiveRecord::Base
  serialize :topic
end

UPDATE

And make your form as:

 %p
   = p.label "Value1"
   = p.check_box(:topic, {:multiple => true}, "value1", nil, name:"project[topic][]")
   = p.label "Value2"
   = p.check_box(:topic, {:multiple => true}, "value2", nil, name:"project[topic][]")
   = p.label "Value3"
   = p.check_box(:topic, {:multiple => true}, "value3", nil, name:"project[topic][]")

and if you get permitted parameter issue add like:

params.permit! #only if you get issue of permitted parameter

Hope this will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

sadly that did not fix it. "syntax error, unexpected ')', expecting =>" because of the " => []" inside the permit of the controller and :topic[] is giving an error in the view aswell
it works with permit! inside the controller ( name wasnt needed ), but i want to work with whitelisting of the permit, so i have to look into that how to do that with arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.