Ruby on Rails - parameter gives nil result back - ruby-on-rails

parameter
"day" => "2013-11-21"
controller
date = params[:day] # gives me a nil value back
#event = Event.new(date: date, id: date.id)
view
...
<% f.hidden_field :id, :value => :day %>
...
Here are the parameters:
{"utf8"=>"✓",
"authenticity_token"=>"lxKzvpGx8nmutI8X8sdZGNaKZ8w1kJEdF/B8ixtqpqA=",
"event"=>{"title"=>"",
"description"=>"",
"day"=>"2013-11-21",
"start(1i)"=>"2013",
"start(2i)"=>"11",
"start(3i)"=>"22",
"start(4i)"=>"08",
"start(5i)"=>"00",
"end(1i)"=>"2013",
"end(2i)"=>"11",
"end(3i)"=>"22",
"end(4i)"=>"08",
"end(5i)"=>"00"},
"commit"=>"Create Event"}
why gives it to me a nil value back?
let me know if you need something more

You should use params[:event][:day] instead of params[:day]

I see at least three problems.
Your param is accessible through params[:event][:day], as you should see in the parameters list you posted.
The hidden field you posted doesn't set params[:event][:day] parameter. Actually, it does nothing as it isn't even being rendered (because of <% usage, instead of <%=.
You try to misuse this parameter, calling id on it (in #event = Event.new(date: date, id: date.id) line). Even if you refer to that parameter properly, it will raise an error.

Related

How to build a model attribute as string using checkboxes (ruby on rails)

I have a welding_certificate model with bw_positions attribute.
I want to be able to pick welding positions through checkboxes
<%= check_box_tag "bw_positions[]", "PA" %>PA
<%= check_box_tag "bw_positions[]", "PB" %>PB
<%= check_box_tag "bw_positions[]", "PC" %>PC
and store them as string for example "PA, PC" or "PA, PB, PC".
The parameters sent to the controller are looking allright:
#<ActionController::Parameters {"_method"=>"patch", "authenticity_token"=>"NSQ9FlN3hqaLbgCkVkDKHgsLOdTp20hkDpXtwqh7ZgiWOB04H0qg6s9_mpyTk03SdFGHRH4paVkA7ZiXvaoCNQ", "welding_certificate"=>{"code"=>"PN EN ISO 9606-1", "number"=>"1234", "material_group"=>"FM5, FM4", "methods"=>"141", "bw"=>"1", "fw"=>"0", "diameter_min"=>"1", "diameter_maks"=>"2", "thk_min"=>"1", "thk_maks"=>"2", "valid_since"=>"2021-04-06", "valid_until"=>"2021-04-28"}, **"bw_positions"=>["PA", "PB"]**, "commit"=>"Zatwierdź", "controller"=>"welding_certificates", "action"=>"update", "welder_id"=>"1", "id"=>"1"} permitted: false>
But the data does is not saved to the database...
Update 07.04.2021
My strong params function looks like this
def welding_certificate_params
params.require(:welding_certificate).permit(:number, :valid_since, :valid_until, :material_group, :diameter_min, :diameter_maks, :thk_min, :thk_maks, :methods, :code, :bw, :fw, :body_id, :bw_positions, :fw_positions, :prolongation_date)
end
I have changed check boxes so they look like this
<%= check_box_tag "welding_certificate[bw_positions]", "PA" %>PA
<%= check_box_tag "welding_certificate[bw_positions]", "PB" %>PB
and this way the data is sent to database but only as one of the array's item not whole string. If I change to
<%= check_box_tag "welding_certificate[bw_positions][]", "PA" %>PA
<%= check_box_tag "welding_certificate[bw_positions][]", "PB" %>PB
i am getting
Unpermitted parameter: :bw_positions
in logger...
I think the very last part of the parameters object in your post may be part of the problem: permitted: false means the params haven't been filtered through strong parameters.
I'm guessing there is a method in your controller named welding_certificate_params that you need to change to something like the following:
def welding_certificate_params
params.require(:welding_certificate).permit( [whatever other params you have], :bw_positions => [] )
end
See the api on strong parameters for more info
sam - thank You
My controller
def welding_certificate_params
if params[:bw_positions]
params[:welding_certificate][:bw_positions] = Array(params[:bw_positions]).join(", ")
end
params.require(:welding_certificate).permit(:number, :valid_since, :valid_until, :material_group, :diameter_min,
:diameter_maks, :thk_min, :thk_maks, :methods,
:code, :bw, :fw, :body_id, :bw_positions, :fw_positions,
:prolongation_date)
end
My View
<% positions = ["PA", "PB", "PC", "PD"] %>
<% positions.each do |position| %>
<%= check_box_tag "bw_positions[]", position %><%=" #{position}"%>
<%end%>
It works finally :D

Ruby On Rails: How to pass an Input value from a form to a controller in a link_to tag?

I have a form with input tag which takes a value from the user and next to that there is a link which should take that value and pass it to a method in a controller which generates an encrypted values based on that hash.
However params[:hash] which is my input does not pass input to controller. gives NULL. Please look in to my code and let me know where I am going wrong.
thanks
view:
= simple_form_for(#user, :html => {:class => 'user_form'}) do |f|
= f.input :hash, :input_html => {:class => 'span4'}
= link_to('Click to confirm key Encryption',confirm_encrypt_path(#user,:hash=>?)
controller:
def confirm_encrypt
check = params[:hash]
puts check # gives null value
MyModel.reset_authentication_pin!(current_user.id,params[:hash])
end
I Expect the puts check value should be the user entered value.
It's hard to tell what's happening here because it appears you've only included a portion of the view, if you could edit your question, that might help shed some light on what else is happening.
Three things that stand out to me are:
In your view you call the input :hash, but when you attempt to get the value you get params[:temp1] not params[:hash]
In the link_to you include the user, but you don't include any value for the :hash. Off the top of my head, I believe you just need to do params[:hash] to successfully get the :hash value through to the controller with the way you have it.
In your view you set up a simple_form_for, but it appears that you don't use a submit to get the :hash value in the controller. If you decided to go that route, you'd just have to make sure that the form is being directed to the proper controller.

Use value from parameter

Ruby on Rails 4
I am passing a hidden value from my form.
<%= f.hidden_field :applied_program, :value => #applied_program.id %>
This is what is posted:
"applied_program"=>"891"} ...
ActiveRecord::RecordNotFound (Couldn't find AppliedProgram with 'id'={"applied_program"=>"891"})
I am trying to update attributes of the applied program in my controller.
I don't know how to assign the parameter to the record. This is what I am trying, but it can't find it based on the parameters passed.
I also tried passing just #applied_program without the .id but then I have the same problem finding it with the Object information. :/
#applied_program = AppliedProgram.find(applied_program_params)
The controller white list:
def applied_program_params
params.require(:payment).permit(:applied_program)
end
Try this
#applied_program = AppliedProgram.find(payment_params[:applied_program])
def payment_params
params.require(:payment).permit(:applied_program)
end
Your data will be passed like below from the form to the controller
{:permit => {:applied_program => 891}}
UPDATE:
applied_program_id = payment_params.delete(:applied_program)
#payment = Payment.new(payment_params)
#applied_program = AppliedProgram.find(applied_program_id)

Encoding UTF-8 in Rails form parameter name

I'm having a problem with my params.
I'm receiving the following parameters:
{"utf8"=>"✓", "authenticity_token"=>"...=", "Portugu\xC3\xAAs"=>{"title"=>"313" } }
In my controller I need to use the key => "Portugu\xC3\xAAs", but first I need it to be in the right form (that is -> Português) and I don't know how can I do that.
EDIT:
Workflow
1. The user saves a language
2. I use that language in a form to save information, like this:
Português[title]
3 . Because the user can have multiple locales in that form (all the locales saved in step 1)
locales.each do |locale|
...
:value => params[locale.key][:title]
The problem is that locale.key ('Português') doesn't match with "Portugu\xC3\xAAs" so it crashes with nil
Can you help me with this?
Thank you
I've tried this, and the result is good:
<% p = {}
p["Português"] = {}
p["Português"][:title] = "Title in Portugês" %>
<p><%= p["Portugu\xC3\xAAs"][:title] %>
And I get
<p>Title in Portugês</p>
I don't see the problem.
The solution that worked for me was iterating the received params and with the help of URI.escape compare the string, if matches enc_locale is set and used in the value.
Thanks to everyone that helped!
enc_locale = ""
params.each do |param|
if URI.escape(param[0]) == URI.escape(locale.key)
enc_locale = param[0]
end
end
...
:value => params[enc_locale][:title]

Update_attributes with a Date field

I ask the user to choose hos borthday from a date select :
<%= select_date Date.today, :prefix => :birthday %>
So in my action, i got a post like this :
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"k6uuDBVy2sM0YU7MtFIk7MsYpTQvQNnW5xuNRwn+OO0=",
"user"=>{"civility"=>"mr",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"current_password"=>"[FILTERED]",
"newsletter_register"=>"1"},
"birthday"=>{"year"=>"2011",
"month"=>"10",
"day"=>"25"},
"commit.x"=>"69",
"commit.y"=>"9"}
But when i use :
#user.update_attributes(params[:user])
The birthday field is not updated. Any idea?
Thank you for help.
If you look at the submitted parameters, the birthday field is not included in the user hash, but rather as a separate field.
You probably want something like this instead (if you give us the code for your current form, I can give a more specific answer):
<%= f.date_select :birthday %>

Resources