How to create/update Rails date_select params? - ruby-on-rails

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.

Related

why the parameters of a submit_tag can be lost?

I have the following button and I do not understand `cause the value of the parameter that I want to send is lost
<%= submit_tag("NOW!", :name=>'fast', :class => 'add-basket-now', :style => "width: 160px;") %>
the rest of the parameters are sent but there I change the default value 'commit' to 'fast' and this is lost and not sent
when I click these are the parameters that are sent
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GX1A5JKigE7paD0SQikdTNTzHQvI/r7JUMx+0NOmFbQ=", "quantity"=>"1", "product_id"=>"1101", "size"=>"3769", "method"=>"get"}
As you can see, no commit or fast parameters are sent, or anything that has to do with the submit_tag.
the controller is very spaghetti but the only place where I should control that parameter would be the following
if params[:fast] != nil
redirect_to checkout_path

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

how to ensure rails multiselect input field passes all values correctly?

so, I've got a form inside a bootstrap modal.
when the modal loads, some javascript calls to the server to figure out what the current user has already chosen (this is an edit modal). once the server response returns, the form select sets itself to the values that the user has already chosen as is demonstrated in the below screenshot.
My problem occurs when the form is submitted. The params being sent to the server have the key 'room_object_id' that points to a single value. Naming issues of that key aside, I need that key to point to an array of values or a string or any structure that will hold multiple values and this is not the case, as demonstrated by the below screenshot.
My form is being generated by
<%= form_for :student, url: student_path, html: {class: 'form-horizontal sync'}, method: :put do |f| %>
<%= f.select(:room_object_id, options_for_select(#rooms.map {|room| [ room['name'], room['objectId'] ]}),{multiple: 'true', include_blank: ''} , {:class => 'select2-init form-control force-full-width', required: 'true', multiple: 'true', name: 'room_object_id'}) %>
<% end %>
I'm a bit unclear how select2 and the html form are working together and I have a feeling my problems are related to this. I'm also a bit unclear of the different usages of rails select, collection_select and options_for_select. I've looked at other SO posts on this topic but nothing pointed me in the right direction. Any pointers would be much appreciated.
Are you using the JavaScript required to run select2?
$(document).ready(function() {
$("#id_of_your_select_field").select2();
});
If you are, is your JavaScript console displaying any errors?

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.

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