Adding additional form depending on the choice of user - ruby-on-rails

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.

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

Change Simple_Form Submit Button Action?

I've created the following SimpleForm for my Rails application. I want the submit button to take the data selected in each of the fields, and carry it over to the next screen. Does anyone know how I can change the property of the SimpleForm submit button? Right now it just seems to create something? I'm a little confused by what it's default action is.
<div class="infoheaders">1. Question
<%= simple_form_for #category do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
simple form for action can be changed to the action(use 'url' option), we want for example,
<div class="infoheaders">1. Question
<%= simple_form_for #category, :url => url_for(:action => 'your_action', :controller => 'your_controller'), :method => 'post' do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
</div>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %>
<br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
In your controller, you will get the params that were send through the form,
class Yourcontroller
def your_action
// render text: params and return false
if params[:category][:ipad].present?
#checked = params[:category][:ipad]
end
if params[:category][:ipad].present?
#checked = params[:category][:android]
end
if params[:category][:ipad].present?
#checked = params[:category][:samsung]
end
/* check the params and assign to the variables */
// render text: params[:category] and return false
end
end
Now create a your_action.html.erb and you will get values there.
<div>
The checked valus is <%= #checked %>
</div>
Thats it.
Replace Your form with the following
<div class="infoheaders">1. Question
<%=form_for #category, :url=>"/register", do |f| %>
<%= f.collection_select(:title, Category.all, :title, :title) %>
<div class="grey-text-subheader">Device: (optional)</div>
<%= f.check_box :Apple %> <%= f.label "Apple" %>
<%= f.check_box :iPhone %> <%= f.label "iPhone" %>
<%= f.check_box :iPad %> <%= f.label "iPad" %>
<%= f.check_box :Mac %> <%= f.label "Mac" %>
<%= f.check_box :Android %> <%= f.label "Android" %><br><br>
<%= f.check_box :Samsung %> <%= f.label "Samsung" %>
<%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>
<%= f.check_box :Windows %> <%= f.label "Windows" %>
<%= f.check_box :Google %> <%= f.label "Google" %>
<%= f.button :submit, 'Submit' %>
<% end %>
</div>
And in routes Add the following lines
get "/register"=> "controller#action"

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

If statement preventing submit button to work

My submit button won't work because of the first if statement. If I remove the block, the button works. The statement seems to break the view. Why?
<% if params[:action] == "edit" %>
<div class="field">
<%= form_tag :action => "edit" do %>
<%= select_tag :vehicle_id, options_from_collection_for_select(#vehicles, :id, :model, params[:id].to_i), :onchange => "this.form.submit()" %>
<% end -%>
<%= link_to 'Nouvelle voiture', new_vehicle_path %>
</div>
<% end -%>
<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>
<% if params[:action] == "edit" %>
<%= link_to 'Détruire', #vehicle, method: :delete, data: { confirm: 'Êtes-vous sûr ? Les trajets associés seront aussi détruits.' } %>
<h1>Trajets</h1>
<div>
<span>Clients</span>
<span>Kms aller/retour</span>
</div>
<%= f.fields_for :trip_distances do |builder| %>
<div class="field">
<%= builder.text_field :id_contract %>
<%= builder.number_field :length %>
</div>
<% end -%>
<% end -%>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
When you put if statement you don't have form in which your submit button resides. Without if statement your form is in the view, and you can submit to it.
Found the solution. I didn't need the form_tag:
<%= form_tag :action => "edit" do %>

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.

Resources