Rails - Combobox Collection depend on another Combobox - ruby-on-rails

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
}

Related

How to get the value of previous select option in form_for rails?

<%= form_for #bug do |f| %>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="form-group">
<%= f.label :Bug_Type %>
<%= f.select :bugtype, [["Feature","Feature"], ["Bug","Bug"]], required: true %>
</div>
<div class="form-group">
<% if f.object.bugtype == 'Feature' %>
<%= f.label :Bug_Status%>
<%= f.select :status,
[
["New","New"],
["Started","Started"],
["Resolved","Resolved"]
],
required: true %>
<% else %>
<%= f.label :Bug_Status %>
<%= f.select :status,
[
["New","New"],
["Started","Started"],
["Completed","Completed"]
],
required: true %>
<% end %>
</div>
<%= f.submit %>
<% end %>
I have this code in my form. I want to know the value user selects for :bugtype, in order to display status accordingly. I am not able to access value of bugtype using instance variable or f.object.bugtype

Adding additional form depending on the choice of user

I'm stacked.
here is code
<div class="form-group">
<%= f.radio_button :repeat, 'once', checked: true%><br>
<%= f.label :repeat_once, 'once'%><br>
<%= f.radio_button :repeat, 'daily'%><br>
<%=f.label :repeat_daily, 'daily'%><br>
<%= f.radio_button :repeat, 'weekly'%><br>
<%= f.label :repeat_weekly, 'weekly'%><br>
<%= f.radio_button :repeat, 'monthly'%><br>
<%= f.label :repeat_monthly, 'monthly'%><br>
<%= f.radio_button :repeat, 'yearly'%><br>
<%=f.label :repeat_yearly, 'yearly' %><br>
</div>
`<% if #event.repeat == 'daily' %>
<div class="field">
<%= f.label :Repeating_interval_in_days %><br>
<%= f.number_field :interval %>
</div>
<% end %>
<% if #event.repeat == 'weekly' %>
<div class="field">
<%= f.label :Repeating_interval_in_weeks %><br>
<%= f.number_field :interval %>
</div>
<% end %>
<% if #event.repeat == 'monthly' %>
<div class="field">
<%= f.label :Repeating_interval_in_month %><br>
<%= f.number_field :interval %>
</div>
<% end %>
<% if #event.repeat == 'yearly' %>
<div class="field">
<%= f.label :Repeating_interval_in_years %><br>
<%= f.number_field :interval %>
</div>
<% end %>
I want to extra field of choosing interval of repeating when User set a type of repeat. My code doesn't work.

Pass a value to a Form in rails

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

Rails 3.2 is not saving dropdown selection in form after clicking edit

I have a form with input field and dropdown boxes with data from other tables. (joins)
but now when i submit it is good, but when i want to edit the fields the dropdown fields are empty but the text fields aren't
My form code:
<%= form_for #contracten, :html => { :multipart => true } do |f| %>
<% if #contracten.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#contracten.errors.count, "error") %> prohibited this contracten from being saved:</h2>
<ul>
<% #contracten.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="forms">
<div class="field">
<%= f.label :naam %><br />
<%= f.text_field :naam %>
</div>
<br>
<div class="field">
<%= f.label :omschrijving %><br />
<%= f.text_area :omschrijving %>
</div>
<br>
<div class="field">
<%= f.label :organisatie %><br />
<%= f.select :organisatieid, options_for_select(#organisaties.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :vestiging %><br />
<%= f.select :vestigingid, options_for_select(#vestigingens.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :Beheerder %><br />
<%= f.select :persoonid, options_for_select(#personen.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :contractsoort %><br />
<%= f.select :contractsoortid, options_for_select(#contractsoorten.map{ |f| [f.naam, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :datumingang %><br />
<%= f.datetime_select :datumingang %>
</div>
<br>
<div class="field">
<%= f.label :datumeinde %><br />
<%= f.datetime_select :datumeinde %>
</div>
<br>
<div class="field">
<%= f.label :contractduur %><br />
<%= f.number_field :contractduur, :placeholder => 'in jaren' %>
</div>
<br>
<div class="field">
<%= f.label :opzegtermijn %><br />
<%= f.number_field :opzegtermijn, :placeholder => 'In Maanden' %>
</div>
<br>
<div class="field">
<%= f.label :betalingsperiode_eenheid_id %><br>
<%= f.select :betalingsperiodeeenheidid, options_for_select(#betalingsperiodeeenhedens.map{ |f| [f.omschrijving, f.id] }), :include_blank => true %>
</div>
<br>
<div class="field">
<%= f.label :betalingstermijn %><br />
<%= f.number_field :betalingstermijn %>
</div>
<br>
<div class="field">
<%= f.label :pdf %><br />
<%= f.file_field :pdf %>
</div>
<br>
<div class="actions">
<%= f.submit "Toevoegen", :class => 'button3' %>
</div>
</div>
<% end %>
<br>
screenshot:
I hope someone know's what the problem.
Add the :selected => ... to your options_for_select parameters.
E.g:
<%= f.select :contractsoortid,
options_from_collection_for_select(#contractsoorten, 'id', 'naam',
:selected => #contracten.contractsortid),
:include_blank => true %>
The changes are actually saved, but when you render the form, the actually selected value must be specified.

Easy way to connect tables using form in rails

I have two tables, works and artists. Every artist has multiple works. Is it possible to list those artists in a dropdown menu when creating a new work.
This is the way I solve it right now:
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>
<div class="field">
<%= f.label :exhibition_id %><br />
<%= f.number_field :exhibition_id %>
</div>
<div class="field">
<%= f.label :artist_id %><br />
<%= f.number_field :artist_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
The example is exactly what you're looking for.
collection_select(#work, :artist_id, Artist.all, :id, :name)

Resources