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">
Related
In my Rails 5 app I want to have already filed text_field_tag by current_login.user.full_name. Additionally I want to pass current_login.user.id inside this text_field as params[:physician_id]. What I did is pretty simple:
<%= text_field_tag "physician_id", current_login.user.id, class: 'form-control', value: current_login.user.full_name, disabled: true %>
With this code I've got:
pry> params['physician_id']
=> nil
If I add <%= text_field_tag "physician_id", id: current_login.user.id (...) I've got:
pry> params['physician_id']
=> {:id=>70, :class=>\"form-control\", :value=>\"Gary KZM JohnsonR\", :disabled=>true}
How to pass this current_login.user.id as a params['physician_id'] inside text_field_tag? should I use something else?
For reference, the method signature is
text_field_tag(name, value = nil, options = {})
You cannot have both current_login.user.id and value: specified, they both map to value attribute of the input. Also your input is disabled so it is not being submitted with the form.
<%= text_field_tag "physician_id", current_login.user.id, class: "form-control",
value: current_login.user.full_name, disabled: true %>
Either you're looking for a select_field_tag or have separate physician_name input and a physician_id as hidden input
<%= text_field_tag "physician_name", current_login.user.full_name, class: "form-control" %>
<%= hidden_field_tag "physician_id", current_login.user.id, class: "form-control" %>
This will submit as params
{"physician_id"=>"1", "physician_name"=>"name"}
However I suggest you don't do that, as this is not a secure way to deal with current_user attributes. I could submit any id as physician_id and probably get access to records that don't belong to me. You should assign these attributes inside of your controller instead.
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
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]} %>
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:
<%= form_tag do %>
<%= label_tag :name, 'Name: ' %><%= text_field_tag 'name' %>
<%= submit_tag 'submit', disabled: #op %>
<%= label_tag :dis_false, "True" %>
<%= radio_button_tag :dis, :true %><br />
<%= label_tag :dis_true, "False" %>
<%= radio_button_tag :dis, :false %><br />
<% end %>
Routes are set up properly and #op = params[:dis].
Now, when I try to select "false" and click "submit", the button becomes disabled. This should only happen if the optin-box "True" is selected. So I was wondering, what's going on here? I was basically trying to make a simple app that would help me enable/disable a button via an option box.
I think I get your answer, when you send false #op is set to "false" not false.
Try this:
#op = params[:dis] == "true"
This is:
params[:dis] # comes as a string "true" or "false", since these are values radio button returns.
params[:dis] == "true" # is true without quotes when radio button with value "true"
# false, without quotes, otherwise, for example when
# when params[:dis] is "false"
It seems 'dis' would be better suited as a checkbox in this case. Disable/enable can also be handled via javascript / coffeescript. Add this to your js.coffee file in app/assets:
jQuery ->
$("#dis").change ->
$("#submit").attr "disabled", $("#dis").is(":checked")
Or in plain jQuery:
$(function() {
$("#dis").change(function() {
$("#submit").attr("disabled", $("#dis").is(":checked"));
});
});
And update your form fields:
<%= submit_tag 'submit', disabled: #op, :id => "submit" %>
<%= check_box_tag(:dis, '', false) %>