Rails time_select avoid pre-filled and storing on ddbb - ruby-on-rails

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.

Related

Submit generated value to input 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

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.

options_from_collection_for_select in rails 4

I'm building a simple form to let users of my rails 4 site message each other.
In the form, the user can select the recipient of the message with a select list of all the users in the system (it's a small user base).
Right now the form control looks like this, and works fine:
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :first_name), {}, {:class => 'form-control'}) %>
However, there's a "nickname" field in the Users table as well-- I'd like to have the form show the nickname (if it exists) as well as the user's first / last name-- so the value of the select would be:
<option>Snoop Dogg - Calvin Broadus</option>
rather than just
<option>Calvin</option>
Is this possible?
Make a display_name on the Users model
def display_name
nickname || first_name
end
Then update your form to reference it
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :display_name), {}, {:class => 'form-control'}) %>

Rails: how to get fields of rejected submit form?

I got one big form with many fields. There are multiple select_tag fields:
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
Problem: If the form is rejected upon submit, the :new action does not remember what is the previous selected values of select_tags.
Every other field reappears after submit (like :title, :description), but categories_id are lost.
Probable solution: We must add default selection to the select_tag in the options_from_collection_for_select method. How to get that category_id value? How can we access the fields of the previous form?
options_from_collection_for_select(Category.ordered, :id, :name, category_id)
You could store this data in the session and load it in the new action if it exists and clear that from the session.
That way anytime the fields reject the one that is not remembered loads into the session and is loaded into the select on the new action. Otherwise nothing is loaded when this session variable is blank.
Hope this helps.
In my form
#product.categories = []
but
#product.category_ids = [1,42,57]
So I added one messy if statement:
-if #product.category_ids.count == 0 #create new product
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
-else #edit old product, or fixing errors to previous submit
-#product.category_ids.each do |category_id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name, category_id), :prompt=> '-- Select a Category --')

Accept Rails model attribute only if it was previously blank

I have a Rails model (persisted with Mongoid) that can be collaboratively edited by any registered user. However, I want to allow editing any particular attribute only if it was previously blank or nil.
For example, say someone created an object, and set its title attribute to "Test Product". Then another user comes along and wants to add a value for price, which until now has been nil.
What's the best way to do this, while locking an attribute that has previously been entered?
Look into the ActiveRecord::Dirty module for some nice utility methods you can use to do something like this:
NON_UPDATABLE_ATTRIBUTES = [:name, :title, :price]
before_validation :check_for_previously_set_attributes
private
def check_for_previously_set_attributes
NON_UPDATABLE_ATTRIBUTES.each do |att|
att = att.to_s
# changes[att] will be an array of [prev_value, new_value] if the attribute has been changed
errors.add(att, "cannot be updated because it has previously been set") if changes[att] && changes[att].first.present?
end
end
The easiest way, i think, is by checking for it in the form itself.
Just say add :disabled => true to the input field if the person cannot edit it.
<% if #my_object.name %>
<%= f.text_field :name, :disabled => true %>
<% else %>
<%= f.text_field :name, :disabled => true %>
<% end %>
(i think there is a prettier way to write this code)
But by using this the user has a visual feed back that he can't do something, it is always better to not allor something than to give an error message

Resources