Well, let's discuss each line of your code to make it clear where the mistake is.
form_params = [:title, :type, :owner_id, :note, :occurred, :due]
First, you declare an array of symbols and assign that array to form_params
variable. It's just a simple plain array, not an associative one, so you can access its values using indexes only, e.g. form_params[0]
would return :title
, but form_params[:due]
is a runtime error as :due
is not an integer index.
Actually, the purpose of event_params
method should be as simple as stating which parameters you allow to be mass-assigned to your objects, so it should look like
params.permit([:title, :type, :owner_id, :note, :occurred, :due])
Getting further, your aim is to edit parameters which came from client-side. As of various dates formats parsing, please refer to this topic.
Disclaimer: It could be much more elegant to force users to pass due
date in conventional %Y-%m-%d
format. Please give a look to rails dates helper methods.
That parameters reside in params
hash, so you could rewrite appropriate key:
def event_params
params["due"] = Date.strptime(params["due"], "%m/%d/%Y").to_s
# here we've converted date format to standard %Y-%m-%d
params.permit([:title, :type, :owner_id, :note, :occurred, :due])
end
This should do two things:
- Replace user's
due
with appropriate date format
- Allow enumerated keys to be mass-assigned for your object
And the rest of your code should work fine with this correction:
def create
@todo = Event.new(event_params)
...
However, beware of side effects:
- Using
event_params
restricts you from getting actual %m/%d/%Y
user input now.
- Malefactor would be able to pass some unparsable string instead of
%m/%d/%Y
formatted date and it would end in runtime error.