Update_attributes with a Date field - ruby-on-rails

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 %>

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

Passing HTML name value into rails select field

I have a form that has a select field where the user selects their title. I want to pass a name value into the rails code so the field can be modified with a client-side validation framework. The client-side validations validate each field based on the name of the field.
My current solution isn't passing the name value of 'title' into the final HTML rendered in the browser.
The field
<%= f.select :title, options_for_select([["Title", "0"], ["Mr.", "Mr."], ["Mrs.", "Mrs."], ["Ms.", "Ms."], ["Dr.", "Dr."], ["Prof.", "Prof."]], selected: "0", disabled: ["0"]), :name => "title" %>
The current HTML output
name="user[title]"
<% possible_options = [["Title", "0"], ["Mr.", "Mr."],
["Mrs.", "Mrs."], ["Ms.", "Ms."], ["Dr.", "Dr."], ["Prof.", "Prof."]]
%>
<%= f.select :title, options_for_select(possible_options), {}, {:name => "title"} %>
It's better to prepare this possible_optins list in controller action itself & accessing here just like an instance variable.
Just for a quick fix, let it be in this view/form.

How to create/update Rails date_select params?

The javascript function enables the field with the checkbox, but then the value for the field isn't passed to the controller on submit. How can I fix this?
<%= f.label :texture_date, "Texture" %>
<%= f.date_select( :texture_date,
{ :order => [:month, :day], disabled: true },
{ class: 'texture_date' }) %>
<%= check_box_tag :texture_date_enable, '' %>
$(document).on('change','#texture_date_enable', function(){
$("select.texture_date").prop('disabled', !this.checked);
});
UPDATE: The param is actually going to the controller, but it isn't updating. It's also sending :texture_date_enable as a param, which isn't wanted.
Here's what the params look like in the log:
"texture_date(2i)"=>"2", "texture_date(3i)"=>"2"
Is the (2i) and (3i) causing problems?
UPDATE:
Full params. Notice that "Date received in shop" is passing, but this field was enabled on page load. All the other date fields are optional, and they aren't getting updated.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rg8yJ4hKAOeUEKytzDq3mc5SwPdnNQitFOlK6VrfU2p49JmX9Ipu0M+dT2GWO+LttS+BMy+5nJ5WXGBo63yh9A==", "ticket"=>{"date_received_in_shop(2i)"=>"2", "date_received_in_shop(3i)"=>"2", "date_received_in_shop(1i)"=>"2015", "job_number"=>"", "customer"=>"", "address1"=>"", "city"=>"", "manufacturing_location"=>"D", "mapsco_page"=>"", "builder_contact"=>"", "builder_contact_phone"=>"", "salesman"=>"", "draftsman"=>"", "door_style"=>"", "door_manufacturer"=>"D", "insulation_and_or_inspection"=>"None", "texture_date(2i)"=>"2", "texture_date(3i)"=>"2", "notes"=>"", "block_up_for_floor"=>"0"}, "texture_date_enable"=>"", "commit"=>"Update Ticket", "id"=>"20"}
The problem was the hidden/disabled Year field. By adding the year field to the other enabled inputs, the form worked as expected.

Ruby on Rails - parameter gives nil result back

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.

RubyOnRails | ActiveRecord::AssociationTypeMismatch

I have a Stage Model, which belongs_to User Model. User Model has_many Stages.
When I try to create a New Stage, with an hidden_field containing the id of current_user ,
I have the following error :
ActiveRecord::AssociationTypeMismatch in StagesController#create
User(#63609336) expected, got String(#16545012)
Here's the array :
{"utf8"=>"✓",
"authenticity_token"=>"YD3VVr9Hntg/LEGjFAzu2roR5NGe6FPclew+zJOvGpY=",
"stage"=>{"user"=>"8",
"intitule"=>"er",
"entreprise"=>"er",
"secteur"=>"er",
"mission"=>"er",
"duree"=>"erea",
"annee_debut"=>"az",
"annee_fin"=>"z",
"comment"=>"az",
"site_entreprise"=>"az"},
"commit"=>"Create Stage"}
user_id is an Integer field.
The view :
<%= f.hidden_field :user, :value => current_user.id %>
Where is the problem ? I can't find any solution !
Thanks for your help.
replace with
<%= f.hidden_field :user_id, :value => current_user.id %>
I wouldn't recommend using hidden fields for this, as hidden fields can be modified by the user. I recommend doing something like current_user.stages.create in the controller instead, which will pre-populate the user_id in the created Stage.

Resources