In Rails 5.1 I have a dropdown created with time_select helper from a table column of "time" data type:
<div class="col-md-12">
<%= form_for(Sleep.new) do |f| %>
<%= f.time_select :hours, {minute_step: 5, prompt: true, order: [:hour]} %>
<%= f.submit "Submit" %>
<% end %>
</div>
The prompt sets the dropdown to "Hours" string by default. In html, this line doesn't have any value:
<select id="sleep_hours_4i" name="sleep[hours(4i)]">
<option value="">Hour</option>
<option value="00">00</option>
However, when a user doesn't choose anything from the dropdown and submits it with the default "Hours" option, the form sends the value to the db:
0001-01-01 00:00:00
Since the value of "Hours" isn't nil, my validation (which should give user a notice if nothing was selected from the dropdown) doesn't work.
How can I still have "Hours" default but set this option's value to nil (prohibit sending any values to the db on submission of this option) and be able to give user a notice like "Choose an hour!" in this case?
It turned out that all you needed to have a default prompt that sends null to db if no option is selected and sends value to db if an option is selected is to add ignore_date: true to f.time_select attributes. So I just turned
<%= f.time_select :hours, {minute_step: 5, prompt: true, order: [:hour]} %>
to:
<%= f.time_select :hours, {minute_step: 5, prompt: true, ignore_date: true, order: [:hour]} %>
Related
I have this code and I want to make it display with checkboxes instead of the dropdown menu. Can that be achieved? .
<%= f.select :job_type_cont,[['Full Time','Full Time'],['Part Time','Part Time'],['Permanent','Permanent']],{:include_blank => 'All....'},class:"form-control",id:"bed_room" %>
For that is neccesary a checkbox input
First you need in you controller receive you parameter like an array:
params.require(:model).permit(:others, job_type_cont: [])
And later, you should do an each, like
<% job_type_conts = ['Full Time', 'Part Time', 'Permanent']%>
<% job_type_conts.each do |type| %>
# Edit: Check id and for of label and checkbox input
<%= f.check_box( :job_type_cont, { multiple: true, id: "#{type.gsub(" ","-")}"}, type ) %>
<%= f.label :job_type_cont, type, for: "#{type.gsub(" ","-")}" %>
<% end %>
You can watch a good simple guide
Rails multiple checkboxes of Sophie Déziel.
Hi in Rails how to display text_field values. In my index file I am trying display my values in text_field_tag but here is i am getting values in this format
considering bellow code
1) {:value=>2}
2) {:value=>0.3e2}
But I just want to display in text_field_tag values as
considering bellow code
1) 2
2) 300
How should i reformat it?
is there any other text_field(I don't want to use it in form field this is just index file for display values I don't want to submit it)
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: 2 %></div>
//or
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: expense.amount %></div>
Thanks a lot for your valuable answer :)
As mentioned here, text_field_tag accepts the second argument as the value to field. So, pass the value directly (instead of passing it as a hash):
<%= text_field_tag :amount, 2 %>
Because you are getting hash value as {:value=>2}
also you can convert it to integer
expense[:value].to_i
2.3.4 :003 > 0.3e2.to_i
=> 30
<%= text_field_tag :amount, expense[:value].to_i, class: "your_class", placeholder: 'some placeholder' %>
In case if you want static value
<%= text_field_tag :amount, 2, class: "your_class", placeholder: 'some placeholder' %>
In case if you want to put it in form but not to be submitted make is disable
<%= text_field_tag :amount, expense[:value].to_i, disabled: true, class: "your_class", placeholder: 'some placeholder' %>
Note: disabled field are not to be subjected to submit with form data, however :readonly => true will be wrapped with form datas
in other case just put this field outside the form
I have a form built with the bootstrap_form gem for ruby. within the form I have some radio buttons. the form renders perfectly with the correct labeling in the edit page of the form, however once I save the form and go to the "show" page to see the results, the inputs for the radio buttons are only showing the value of the radio button (which are numbers) and not the custom label name I have assigned. How can I make the custom label appear instead of the value in the show page?
Here is my code:
_form.html.erb:
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 0, label: "Female", checked: true, inline: true %>
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
<% end %>
<%= f.form_group :nationality, label: { text: "Nationality" } do %>
<%= f.radio_button :nationality, 0, label: "Kuwaiti", checked: true, inline: true %>
<%= f.radio_button :nationality, 1, label: "Non-Kuwaiti", inline: true %>
<% end %>
show.html.erb
<p><strong>Full Name:</strong> <%= #profile.name %></p>
<p><strong>Civil ID no:</strong> <%= #profile.civil %></p>
<p><strong>Gender:</strong> <%= #profile.gender %></p>
<p><strong>Nationality:</strong> <%= #profile.nationality %></p>
<p><strong>Mobile no:</strong> <%= #profile.mobile %></p>
<p><strong>Personal Email:</strong> <%= #profile.personal_email %></p>
Thanks for the help in advance!
Update:-
New form code:-
<%= f.form_group :gender, label: { text: "Gender" } do %>
<%= f.radio_button :gender, 1 %>
<%= f.label 'Female', inline: true, checked: true %>
<%= f.radio_button :gender, 0 %>
<%= f.label 'Male', inline: true %>
<% end %>
Tried this suggestion but still getting the same problem, only the the radio buttons are no longer in line and have incorrect formatting.
You need a lookup table
You have saved numeric values that correspond to the radio button. So, when you display the variable in the show.htm.erb it is showing the numeric values retrieved from the database record. This makes sense. But you need the string associated with the number, which requires a lookup table
Creating labels in the form does not create custom labels for your field. To create custom field labels, you would need to setup i18n localizations for your activerecord models, which is a good solution, but also one that will take some time and learning curve.
Creating a hash-based lookup table.
Active Hash gem
You could write your own lookup helper or use this gem which simplifies implementing hash-based lookup tables.
Example
# in app/models/person.rb
class Gender < ActiveHash::Base
self.data = [
{:id => 0, :gender => "Female"},
{:id => 1, :gender => "Male"}
]
end
From their spec
We wrote ActiveHash so that we could use simple, in-memory, ActiveRecord-like data structures that play well with Rails forms...
Build error in gem (Update)
I just checked an it seems their build has an error. Probably best to avoid for now.
Using view helpers to translate boolean values to strings (Added)
You could implement helpers your /helpers/profile_helper.rb. I call them "lookups," but that is probably not standard.
For each of the boolean you need to translate to strings create a helper
#profile_helper.rb
# Each of your booleans
def gender_to_s(value)
value ? "Male" : "Female"
end
def nationality_to_s(value)
value ? "Kuwaiti" : "Non-Kuwaiti"
end
# Generic true/false conversion
def boolean_to_s(value)
value ? "Yes" : "No"
end
Note: I would name the helpers consistently to match the field name, so in your view it is very obvious which "to_s" to invoke.
In your view
<p><strong>Gender:</strong> <%= gender_to_s(#profile.gender) %></p>
Note: If you wanted a single view helper, this could be implemented in application_helper.rb and include a passed parameter "type" as a switch control, but I think the most straightforward solution is to create a view helper for each field.
You want a separate tag for the labels. Instead of:
<%= f.radio_button :gender, 1, label: "Male", inline: true %>
You need:
<%= f.radio_button :gender, 1 %>
<%= f.label 'Male, inline: true %>
Might also be worth looking at this answer: Bootstrap 3: does form-horizontal work for radio buttons with a control-label?
It doesn't use the form label helper, but it looks like it does what you need.
OK one more approach so that you have a full set to choose from. In Rails 4.1, enum data types were implemented in ActiveRecord as described in section 2.5 of the release notes. If you are running Rails 4.1+, you can take advantage of this new feature.
Essentially, you will need to write migrations to change your boolean and other data types to enum. For instance, for the gender example, rather than having gender of 0 signify female and 1 signify male, and maintain a mapping for that, you could simply have:
enum gender: [:female, :male]
and likewise for your other attributes.
This should ultimately be easier to maintain and will help keep your application code as transparent as possible.
<label><%= form.radio_button :gender, 1, hide_label: true, inline: true%>Female</label>
<label><%= form.radio_button :gender, 0, hide_label: true, inline: true%>Male</label>
worked for me Bootstrap 4.1
I want to save the text_field to database with defaut value ,but it's not work.
<p>
<%= f.label :用户id %><br>
<%= f.text_field :user_id ,:value => "#{current_user.try :id}", disabled: true %>
</p>
<p>
<%= f.label :用户昵称 %><br>
<% user = User.find current_user.id%>
<%= f.text_field :name ,:value => user.name , disabled: true%>
</p>
Change disabled: true to readonly: true if you want the field to be un-editable but still submit a value.
"READONLY and DISABLED both remove the functionality of the input field, but to different degrees. READONLY locks the field: the user cannot change the value. DISABLED does the same thing but takes it further: the user cannot use the field in any way, not to highlight the text for copying, not to select the checkbox, not to submit the form. In fact, a disabled field is not even sent if the form is submitted."
Reference:
http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html
http://www.w3.org/TR/html4/interact/forms.html#h-17.12
Also see this answer: https://stackoverflow.com/a/7730719/2113461
I have this textfield that I want to always have this value:
<%= text_field_tag :quantity, "1", class: "uneditable-input" %>
i.e. I don't want the user to be able to change the quantity value to anything other than 1.
I tried adding disabled: true and that worked to grey out the field, but it also disabled it - changing the behavior of the form (i.e. the form was submitted without a quantity value).
All I want to do is to force every person that submits this form to be able to see the Quantity of 1 - and not be able to change it - and have the system process quantity of 1.
How do I do that?
Try this <%= text_field_tag :quantity, "1", class: "uneditable-input", :readonly => true %>
or if you want to disable it you can do it this way
<%= text_field_tag :quantity, "1", class: "uneditable-input", :disabled => true %>
Just try:
<%= text_field_tag :quantity,:readonly => true%>
You can simply set the text field value to 1 server side, leaving disabled: true client side.
Try:
<%= text_field_tag :quantity, nil, {value: "1", disabled: true} %>
Output:
<input disabled="disabled" id="quantity" name="quantity" type="text" value="1">