how to do dropdown select in rails form ?
i tried to
<div class="field">
<%= f.label :car_id %>
<%= select("car", "car_id", #cars) %>
</div>
<div class="field">
<%= f.label :firstname %>
<%= f.text_field :firstname %>
</div>
<div class="field">
<%= f.label :lastname %>
<%= f.text_field :lastname %>
</div>
<div class="field">
<%= f.label :dateofbirth %>
<%= f.date_select :dateofbirth %>
</div>
but i get this error
undefined method `empty?' for nil:NilClass
at this line.
<%= select("car", "car_id", #cars) %>
Try
<%= f.select :car_id, #cars.collect { |car| [car.name, car.id] } %>
You need to set the #cars instance variable in the controller method for the above line of code to work.
I am assuming that car has a name field. Replace name with a field of your Car model that is good enough for a label.
For every option in the selectlist, name will be set as the label and id will be set as the value.
For more, read the documentation here
Related
I have to pass a value in a form where the user cannot to choose. I have a Register model with some fields. One of them is a value from another Model called Car. I show in the _form view the car plate value but I want to link this value in the form This is the code in _form :
<%= form_for(#reg) do |f| %>
<div class="field">
<p><%= f.label :date %></p><br>
<%= f.date_field :date_reg %>
</div>
<div class="field">
<p><%= f.label :car_id %></p><br>
<% Car.all.each do |car| %>
<%= car.plate %>
<%= f.select(:driver_id,
options_from_collection_for_select(Driver.all, :id,
:name), {:prompt => 'Please Choose'}, :class => "form-
control") %><br>
<% end %>
</div>
<div class="field">
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Thanks all
I am looking for a way to change the Combobox Collection (combo_two) depends on the option choose in another combobox (combo_one). Follow bellow the Rails form:
<%= form_for(#requisite) do |f| %>
<div class="field">
<%= f.label :this_type %><br>
<%= f.text_field :this_type, :value => params["type"] %>
</div>
<div class="field">
<%= f.label :this_id %><br>
<%= f.number_field :this_id, :value => params["id"] %>
</div>
<div id="combo_one" class="field">
<%= f.label :require_type %><br>
<%= f.select :require_type, ([["Element","Element"],["Research","Research"]]) %>
</div>
<div id="combo_two" class="field">
<%= f.label :require_id %><br>
<%= f.collection_select(:require_id, #elements, :id, :name, :prompt => true) %>
</div>
<div class="field">
<%= f.label :qty %><br>
<%= f.number_field :qty %>
</div>
<div class="field">
<%= f.label :lvl %><br>
<%= f.number_field :lvl %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I hope we resolve it with javascript (jquery), but I do not know javascript (jquery)
with jquery you have posible get a value for first combo box using a html onchange event in combo box
<%= f.select :require_type, ([["Element","Element"],["Research","Research"]]),:onchange => GetValue(this) %>
or
<select class="combo_box" onchange="GetValue(this);"></select>
function GetValue(this){
var value = this.value;
//Another code for second combobox
}
I have a classic form_for:
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
I would like to use another model inside this view, that would also update when the user clicks the submit button. I know this has to do with nested forms but I'm a little confused about how to implement it. Here's the variable from the second model that I would like to add:
<% #trip_distances.each do |t| %>
<%= form_for(t) do |e| %>
<div class="field">
<%= e.text_field t.id_contract %>
<%= e.number_field t.length %>
</div>
<% end %>
Obviously this is not correct. I guess I need to use the field_for method?
Firstly, consider using simple_form gem, as it cleans up a bit the code, and is overall more developer-friendly :)
Then in your view you will have something like
<%= simple_form_for sth do |form| %>
<% form.input :attributeUno %>
<% form.input :length %>
...
Then to add to this form inputs for nested element add simple_fields_for
<% #trip_distances.each do |t|
<%= form.simple_fields_for t do |fields| %>
<% fields.input :length %>
...
And don't forget to add
accepts_nested_attributes_for :something in your model :)
PS: You should also consider using slim or haml to make creating views a bit easier, and also clean them up :)
I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.
I've just added 2 new columns to my user table: photo and sex.
For some reason when I create a new user and fill up the entire form(name, photo and sex) it does not stay saved.
When I go to edit it just shows the name I've filled in, but the photo and sex fields are empty everytime.
Any idea what might be causing this?
Here's the form:
<div class="field">
<%= f.label :name, "Full Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :photo_1, "Photo" %><br />
<%= f.text_field :photo_1 %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>`
Firstly I would rename that column from sex to gender.
Secondly check the attr_accessible declarations on your model, you may need to add these fields to the list