text_field with default value can not save to db in rails - ruby-on-rails

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

Related

Rails require radio button from a collection

I need a way for my form to not be sent if the user didn't bother to select any radio buttons.
I'd like to to that within the view and the controller, not in the model (the data shouldn't even be sent)
<%= form_tag("/bookings/new", method: "get") do %>
<% #flights.each do |flight| %>
<%= radio_button_tag :flight_id, flight.id %>
<% end %>
<%= submit_tag "book now" %>
<% end %>
edit, to clarify
normally I'd do
<%= f.text_field :name, required: true %>
but, as I have many radio buttons and I only need one for the form to work, I don't know how to implement it
You can set validation in the model to see the presence of checkbox if javascript is disabled. This is a more robust method.
validates :flight_id, :acceptance => true
Docs here - http://guides.rubyonrails.org/active_record_validations.html#acceptance
Edit
function validateCheckBox() {
var x = document.getElementById("flight_id").checked;
if(!x) {alert("Not checked")}
}
<%= submit_tag "book now" , :onclick => "validateCheckBox();" %>
<%= f.text_field :name, required: true %>
This still works perfectly for radio buttons, and it's okay if it ends up on all radio items. The form will still only require one input.
I just tested it on my Rails 6 app.

Changing dropdown content in spree form (Rails)

I am working on a project that is built on spree and am trying to concatenate state abbreviations to the state's name in the dropdown when selecting shipping address.
Example: "New York - NY" instead of "New York"
The shipping address form can be found here with the code making the state selection dropdown below:
<% if Spree::Config[:address_requires_state] %>
<p class="form-group" id=<%="#{address_id}state" %>>
<% have_states = !address.country.states.empty? %>
<%= form.label :state do %>
<%= Spree.t(:state) %><abbr class='required' title="required" id=<%="#{address_id}state-required"%>>*</abbr>
<% end %>
<%== state_elements = [
form.collection_select(:state_id, address.country.states,
:id, :name,
{include_blank: true},
{class: have_states ? 'form-control required' : 'form-control hidden',
disabled: !have_states}) +
form.text_field(:state_name,
class: !have_states ? 'form-control required' : 'form-control hidden',
disabled: have_states)
].join.gsub('"', "'").gsub("\n", "")
%>
</p>
<noscript>
<%= form.text_field :state_name, class: 'form-control required' %>
</noscript>
After looking at this post, I thought that :name is the method being called to generate the text in each option, but when I changed it to :abbr (which should be a property that it has) everything stayed exactly the same. I am pretty sure that my changes are running since when I make other changes it works. How would you change the option text and managing that, how would you get both the name and abbreviation and concatenate them together with a hyphen.
Just as a note: I am using Deface::Override to make changes to the code.

text_field disabled in Ruby on Rails

I have the following code to disable a text_field when a user is not an admin and it is working fine:
<div class="form-group">
<%= f.label :nome, "Genero" %>
<%= f.text_field :nome, class: "form-control", disabled: true if not is_admin? %>
</div>
But when a user is an admin the text_field just disappears. Does anyone know why this is happening and what I have to do?
Assuming that is_admin? returns true or false, you can simply do
<%= f.text_field :nome, class: "form-control", disabled: !is_admin? %>
Currently, the if condition i.e., if not is_admin? is applied on the entire text field, which results in text field disappearance when is_admin? returns true and when the condition returns false text field is displayed.
The specific reason that your code wasn't working as expected is an operator precedence issue - the if test is being applied to the whole text field, not just the disabled parameter.
The most direct solution to this (though not necessarily the best) is to wrap true if not is_admin? in parentheses:
<%= f.text_field :nome, class: "form-control", disabled: (true if not is_admin?) %>
Otherwise, the whole text field has the if applied to it, like this:
<%= (f.text_field :nome, class: "form-control", disabled: true) if not is_admin? %>
So it will be rendered only for non-admin users - i.e. when is_admin? is false.
Also, when it's rendered, it will always be disabled. (Which on the plus side, will make it harder for non-admins to abuse.)

create form fields on the fly from drop down

I dint even understand how to search for my problem scenario in google.
Problem : When a user selects from a drop-down, the below form fields should change accordingly.
Example : If a user selects "Human" from Drop-down , then below form_fields should be shown like "age" field ,"ethnicity" field etc..
If a user selects "Event" from drop-down, below form fields should show "Date", "venue" etc..
and By default we will have "SUBMIT" button.
Can anyone tell how to achieve this ? Any help with simple_form ruby gem also would be helpful.
You can do this with some simple javascript. Lets say you have a form with the following fields
<%= f.select :some_field, %w[Human Cat], {}, {:class => 'my-select'} %>
<div class="human">
<%= f.label :age %><br>
<%= f.text_field :age %>
</div>
<div class="cat" style="display:none;">
<%= f.label :sub_species %><br>
<%= f.text_field :sub_species %>
</div>
Now add some javascript to make it dynamic
<script>
$(".my-select").change(function(){
if($(".my-select").val() == "Human"){
$(".human").css('display', 'block');
$(".cat").css('display', 'none');
}else{
$(".human").css('display','none');
$(".cat").css('display', 'block');
}
})
</script>
This is quite easy to achieve with the simple_form gem and some simple javascript.
View
<%= simple_form_for #object do |f| %>
<%= f.input :attribute_name, :collection => ['Human', 'Event'], :input_html => { :class => 'dropdown' %>
<div class="toggle human" style="display:none;">
<%= f.input :age %>
</div>
<div class="toggle event" style="display:none;">
<%= f.input :date %>
</div>
<%= f.button :submit %>
<% end %>
The key here is wrapping the fields you want to toggle in div with specific selectors.
Then we look for a change event on the dropdown field, and toggle our divs accordingly.
Javascript
$('.dropdown').change(function() {
// hide divs first
$('form .toggle').hide();
// get value from dropdown
var divClass = $('.dropdown').val().toLowerCase();
// show necessary div
$('.' + divClass).show();
});
Using this approach, you don't need to write the specific case for every value of the dropdown, but can use the lower case value of the dropdown as the selector for the div.
Here is a simple example with static markup.

Changing field type from check box to radio button in _form.html.erb (Ruby on Rails 3)

I am trying to change check_box field to a radio_button field in order to replace true/false value by enumeration. The scaffold generated code was as following:
<!%= f.label :status %><br />
<!%= f.check_box :status %>
And I tried to replace it by this:
<%= f.radio_button 'status', '0' %><%= f.label :status ,'Public' %><br />
<%= f.radio_button 'status', '1' %><%= f.label :status ,'Protected' %><br />
<%= f.radio_button 'status', '2' %><%= f.label :status ,'Private' %>
In MySQL the 'status' field is a tinyint(1), so from DB type perspective it should be ok. However, the evaluation of the fields on the show.html is still true/false and not the expected values - 0,1,2.
I think that this field is configured as a check_box somewhere else in the configurations or code done by Rails, but where is it? Is it possible at all to make such changes? If it is not then what is another painless way to do that?
Tinyint(1) maps to a boolean in rails.
http://www.ruby-forum.com/topic/201859
You should create a migration and change the field to an integer if you wish to use it as an integer.
change_column :my_table, :status, :integer

Resources