Reputation: 131
i have the following form
<%= simple_form_for(@software) do |f| %>
<%= f.input :softwarename, :collection => software_options, as: :check_boxes %>
And this helper.
module SoftwareHelper
def software_options
['7zip','SysInternals','Office','PDF-Creator']
end
end
My application controller looks like this
def software_params
params.require(:software).permit(:softwarename => [])
end
And here is my software controller:
def new
@software = Software.new
end
def create
@software = Software.new(software_params)
@software.save
redirect_to @software
end
When I try to save the form input to my database (sqllite) i get the following error:
TypeError: can't cast Array to string
Do i have to convert the array to a string?
Upvotes: 0
Views: 971
Reputation: 53018
You are receiving error as
TypeError: can't cast Array to string
because the attribute that you are trying to save i.e., softwarename
is of type String
in database and you are passing its value as an Array
from the form (with checkboxes).
What you can do is mark softwarename
attribute for serialization using serialize
method, so that the Array passed as value of softwarename
is converted to a String before saving it in database. This would also take care of deserialization from String
to Array
in case you are retrieving the attribute from database.
class Software < ActiveRecord::Base
serialize :softwarename, Array ## Add this line
## ...
end
Refer to serialize method documentation for more details.
Upvotes: 2