Change Simple_Form Submit Button Action? - ruby-on-rails

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"

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

How save multiple values from array in field?

In model Project i have next array:
PAYMENT_TYPE = ["in bar", "bargeldlose Zahlung", "E-Geld"]
I have table projects with field payments, type: string
When i create new object(model Project)how me add values from array PAYMENT_TYPE in field payments?
In view new.html.erb i added
<%= Project::PAYMENT_TYPE.each do |type| %>
<%= f.check_box :payment, :name => 'project[payment][]', type %>
<% end %>
Displaying error: /home/ubuntu/workspace/app/views/projects/new.html.erb:35: syntax error, unexpected ')', expecting => ...=> 'project[payment][]', type );#output_buffer.safe_append=' ...
full new.html.erb:
<%= form_for #project do |f| %>
<p>
<%= f.label 'Name' %>
<%= f.text_field :title %>
</p>
<P>
<%= f.label 'Budget' %>
<%= f.text_field :price %>
für
<%= f.select :price_category, Project::PRICE_CATEGORIES %>
</P>
<p>
<%= f.label 'Description' %>
<%= f.text_area :description %>
</p>
<p>
<%= f.label 'Category' %>
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<p>
<%= f.label 'Skills Freelancer (15 pieces) *' %>
<%= f.text_field :skill %>
</p>
<p>
<%= f.label 'Location' %>
<%= f.text_field :location %>
</p>
<p>
<%= Project::PAYMENT_TYPE.each do |type| %>
<%= f.check_box :payment, :name => 'project[payment][]', type %>
<% end %>
</p>
<p>
<b>Anonymity order</b>
</p>
Setup allows for players to remain anonymous. Note that this may reduce the number of responses.
<p>
<%= f.label 'Place Order anonymously' %>
<%= f.check_box :anonymity %>
</p>
<p>
<%= f.fields_for :documents do |d| %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<% end %>
</p>
<P>
<%= f.submit 'Create' %>
</P>
<% end %>
I can't google the correct syntax for this form, but you can't have naked hash params like you do except in the last parameter of a method call in Ruby. That is, your code is invalid Ruby.

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 %>

How to ensure all nested attributes show during 'edit'

I currently have a nested model User has_many Sales_Orders has_many Items.
I can create the Sales_Order with nested Items properly but when I try to 'edit' the Sales_Order, the Sales_Order information is visible but the Items are not shown. Any idea why these wouldn't show?
[sales_orders_controller.rb]
...
def edit
#sales_order = SalesOrder.find(params[:id])
end
[edit.html.erb]
<% provide(:title, "Edit SO") %>
<h1>Edit Sales Order</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#sales_order) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="span3">
<%= f.label :so, "SO#:" %>
<%= f.text_field :so %>
<%= f.label :customer, "Customer:" %>
<%= f.text_field :customer %>
<%= f.label :enter_date, "Date Entered:" %>
<%= f.text_field :enter_date, value: date_formatter(#sales_order.enter_date) %>
<%= f.label :request_date, "Request Date:"%>
<%= f.text_field :request_date, value: date_formatter(#sales_order.request_date) %>
<%= f.label :comments, "CS Comments:" %>
<%= f.text_area :comments %>
</div>
<div class="span3">
<% f.fields_for #sales_order.items do |builder| %>
<%= render 'item_fields', f: builder %>
<% end %>
</div>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
[_item_fields.erb]
<fieldset>
<%= f.label :item_code, "Item Code:" %>
<%= f.text_field :item_code %>
<%= f.label :qty_in_kg, "Qty (kg):" %>
<%= f.text_field :qty_in_kg %>
<%= f.label :qc_comments, "Comments:" %>
<%= f.text_field :qc_comments %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
<% f.fields_for #sales_order.items do |builder| %>
should be
<%= f.fields_for #sales_order.items do |builder| %>
Without the = you're building the output in the loop but never rendering/printing the return value (the generated HTML output).
Use <%= f.fields_for #sales_order.items do |builder| %>
Notice the = sign. This implies, "evaluate and embed"

Nested_form_for with field_for not working

my view is
<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for #item_category, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
<div><%= f.label :item_name %>
<%= f.text_field :item_name %></div>
</br>
<div>
<%= f.fields_for :item_companies do |f| %>
<%= f.label :company_name %>
<%= f.text_field :company_name %>
</div>
</br>
<%= f.fields_for :item_weights do |f| %>
<div>
<%= f.label :weight %>
<%= f.text_field :weight %>
</div>
</br>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<%end%>
<%end%>
<div><%= f.submit "Submit" %></div>
<% end %>
</div>
and controller
def new
#item_category = ItemCategory.new
item_company = #item_category.item_companies.build
item_company.item_weights.build
end
and when I have applied debugger in create action and I saw value of params[:item_category] and this is generated {"item_name"=>"iname", "item_companies_attributes"=>{"0"=>{"company_name"=>"cname", "item_weights_attributes"=>{"0"=>{"weight"=>"20kg", "price"=>"100"}}}}}
ItemCategory.new(params[:item_category])
generated error
ActiveModel::MassAssignmentSecurity::Error Exception: Can't mass-assign protected attributes: item_companies_attributes.
Where i am wrong and how to save value in three tables using this.
It seems that you forgot to add this piece of code in your ItemCategory model:
attr_accessible :item_companies_attributes
which will allow you to set these attributes.

Resources