Submit generated value to input Rails - ruby-on-rails

I am creating a form in which an input in that form auto-generates a unique group_id. I set the value of that input to the randomly-generated string and set the input field to disabled so they can not edit it unless they click a check-box.
When I submit the form however the value does not go into the database even though I have the :group_id set in the permit params.
<%= form.text_field :group_id, id: :group_id, class: 'form-control inline', value: "DSC-" + "#{#pdnum.id}".rjust(3, '0').to_s, disabled: true %>
Does the value not get submitted into the database? If not, how can I get around this so that this randomly generated string is placed into the database?

Try to use readonly: true instead of disabled

Related

Rails/ActiveAdmin form: how to disable an input field based on the value of another (boolean) input field?

I have a model that has a boolean field and an array field that gets the values populated from another model (foreign key, not really relevant to my question).
In ActiveAdmin, I have a form like such:
form do |f|
f.semantic_errors
f.inputs do
f.input :boolean_field_name
f.input :array_field_name,
as: :searchable_select,
ajax: true,
input_html: { disabled: true }
end
f.actions
end
The disabled: true works, but I would like to replace the true with something that evaluates whether or not the input checkbox for boolean_field_name has been checked on the form (which by default it isn't).
I've tried params[:boolean_field_name], params.key?[:boolean_field_name], f.object.boolean_field_name, f.object[:boolean_field_name], resource[:boolean_field_name] and resource.boolean_field_name, but they all do nothing and evaluate to nil as far as I can tell.
I've even tried ModelName.find(params[:id]).boolean_field_name but of course since params[:id] is nil that doesn't work, and it wouldn't find a record with that id anyway because the record hasn't been created yet.
I've tried looking through the ActiveAdmin repository but I can't find the information I'm looking for in the source code either.
Is this even possible?
Did you mean like this?
form do |f|
f.semantic_errors
f.inputs do
f.input :boolean_field_name
f.input :array_field_name,
as: :searchable_select,
ajax: true,
input_html: { disabled: f.object.boolean_field_name # <= does not work }
end
f.actions
end
Since the information is a bit ambiguous to me, I would firstly like to know:
From the description:
The disabled: true works, but I would like to replace the true with
something that evaluates whether or not the input checkbox for
boolean_field_name has been checked on the form (which by default it
isn't).
Did you mean that you wanna change the disabled attribute depending on the other form field after the page loads? If that's true, then you have to do it with javascript as something like this:
let booleanField = document.querySelector('booleanField'),
arrayField = document.querySelector('arrayField');
booleanField.addEventListener('change', function(e) {
arrayField.setAttribute('disabled', booleanField.value);
})
Or if you mean you just wanna set the value of the disabled attribute to what boolean_field initially is on page loads, it will bring us more information if you can debug with the tools like debug, debugger or binding.pry. It will be helpful to checkout what f.object.boolean_field_name returns. According to the information you provides, I guess it could really be nil.

Rails time_select avoid pre-filled and storing on ddbb

my time select is being pre-filled with the current hour:
<%= form.time_select :afterstart, class: "form-control" %>
How can I have a blank placeholder (default to null) so it doesn't get stored in ddbb unless the user fills it in?
Couldn't find any usable method in the API docs.
Thanks!
you can use include_blank: true to avoid pre-filled
<%= form.time_select :afterstart, include_blank: true, class: "form-control" %>
but the time_select helper will automatically add default time, so the attribute afterstart of your model still be saved to db, to avoid this, you can merge your model params with blank afterstart(1i) to afterstart(3i)
# your-model controller
private
def your_model_params
params.require(:your_model).permit(...,:afterstart).merge!({
"afterstart(1i)": "",
"afterstart(2i)": "",
"afterstart(3i)": ""
})
end
so that if user does not select time, that mean afterstart(4i) and afterstart(4i) blank -> your model afterstart will be nil and not to be saved to db.
another way is to check if 4i && 5i blank then reject all params keys start with afterstart.

Rails Form Select Requirement with partial

I'm creating a form with a .select field that loads a list of states via partial. The requirement isn't being enforced on state and I'm not sure why. It lets you submit the form with the default blank value 'State'
Would appreciate any help figuring out where my syntax is wrong on this form? If this looks foreign, using SLIM instead of HTML.
= f.select :state, nil, include_blank: 'State', required: true # not working
= render partial: 'addresses/states'
= f.text_field :zip, placeholder: 'Zip', required: true, pattern:'[0-9]*' # works
The states partial looks like this:
option value="AL" AL
option value="AK" AK
option value="AZ" AZ
option value="AR" AR
...

Ruby on rails simple form text field value is overridden by default value

I am using a simple form like which has a text field with a default value. Once the user sets their desired value, the default value should be overridden by the user's desired value. But each time the user opens the form to edit it, they see the default value again and again:
<%= f.input :notes, input_html: {:value => #order_f.decorate.template_message, rows: 12} %>
Instead of setting a value, try setting a placeholder like so:
<%= f.input :notes_to_deliverer, placeholder: #order_f.decorate.deliverer_template_message, input_html: {rows: 12} %>
If you don't need the placeholder then you can do it like this:
<%= f.input :notes_to_deliverer, input_html: {:value => object.notes_to_deliverer.present? ? object.notes_to_deliverer : #order_f.decorate.deliverer_template_message, rows: 12} %>
Here the object is for which you have created the form. Also please make sure that this value is saved in database on form submit. And if suppose this your default value to be stored what I mean is if user doesn't enters any value then you need this to be stored then it would be better to use default value at database end. You can set it in migration file.
Hope this helps.

Rails Simple Form - Error in dropdown input when selecting prompt message

I am using Rails 4 and Simple Form to create a form where I ask users for a bunch of data. I am including a dropdown selector to a model association in the following way:
<%= f.association :location, collection: Location.order("LOWER(name)").all, required: true, include_blank: false, prompt: "Choose location..." %>
However, I get a undefined method 'name' for nil:NilClass error when the user doesn't actively choose anything and leaves the default prompt message selected in the dropdown.
How can I make the app send the user back to the form and highlight that he needs to choose a location in the dropdown? Just like it happens when you have a required input field and no data is provided...
Thanks!
Adding the required: true in your form doesn't actually make the :location a required attribute on your model.
You need to add the following to your model:
validates :location, presence: true

Resources