I'm new to rails. What I'm trying to do is to design an order system. It lists a bunch of items and each item is followed by a select box so that people can choose the number that they want to order. My question is how to pass this count[] array and the corresponding item id from view to controller using button_to. Thank you for any suggestions.
<% #items.each do |item| %>
<li>
<%= link_to item.name, item %>
<%= select_tag 'count[]', options_for_select([0,1,2,3,4,5])%>
</li>
<% end %>
<%= button_to 'Place Order', orders_confirm_path, method: :post, params: { ??? } %>
You use the tag multi-select.
example in your view. For can select multi with ctrl + click at item:
<%= form_tag orders_confirm_path, method: :post %>
<label>select more than one with ctrl + click at item</label>
<p><%= select_tag :items, Item.all.collect {|item| [item.name, tem.id]}, {prompt: "Select item"}, multiple: true %></p>
<p><%= button_to 'Place Order'%></p>
and your controller you can receive the parameters in this way:
def create
#order = Order.new(params[:items])
if #order.save
code ...
else
code ...
end
end
You can this using form like below
<%= form_tag orders_confirm_path, method: :post do %>
<% #items.each do |item| %>
<%= hidden_field_tag :item_id, value: item.id %>
<li>
<%= link_to item.name, item %>
<%= select_tag 'count[]', options_for_select([0,1,2,3,4,5])%>
</li>
<% end %>
<%= button_to 'Place Order' %>
<% end %>
Now you can find on controller params[:count] params[:item_id]
Hope to help
Related
I am trying to make a front end from which if clicked on a button "Rent", it will render a Modal of a bootstrap in which I need to get current_item in order to rent that item. I am currently getting the latest item instead of current_item.
_itemviewer.html.erb
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', '#rentModal', class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item } %>
<% end %>
<% end %>
inside _rent_modal.html.erb
<%= simple_form_for [current_item, #acquiretime] do |f| %>
<div class = "add_item_form">
<%= f.input :required_time, as: :date, html5: true %>
<%= f.input :return_time, as: :date, html5: true %>
<%= f.hidden_field :user_id, value: current_user.id %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit 'Add Item', class: 'btn btn-primary' %>
</div>
<% end %>
the problem is that you are creating many links to your modal with the same id.
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', '#rentModal', class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item } %>
<% end %>
<% end %>
here's it, for each item you create a new link to your modal with the id #rentModal the last modal is the one of the latest item; that may be the cause of being it rendered.
so you may need to create different modals and links ids for each item.
like the code below.
<% #items.each_with_index do |item, j| %>
<% if item.user != current_user %>
<%= link_to 'Rent', "#rentModal_#{j}", class: 'btn btn-success', "data-toggle" => "modal", "data-target" => "#rentModal_#{j}"%>
<%= render partial: "layouts/rent_modal", locals: {current_item: item, index: j } %>
<% end %>
<% end %>
here you can see that we used j to define the Modal id, you need to use that id also in your modal html part, so in your _rent_modal.html.erb you need to use the same id for each item and that is why we passed index as a local to that partial.. Also you need to wrap your code inside _rent_modal.html.erb inside a div with the modal id. you can see how you define a modal here.
I have a model called account which has_many :options. I want to create a form in which i can list all the options with a checkbox at the side, so the current account can select the options he/she wants inside a form so I can create the has_many relation.
This is what i have
def index
#account = current_account
#options = ['Op 1', 'Op 2', 'Op 3', 'Op 4']
end
and for the view:
<%= form_for(#account, url: options_path) do |f| %>
<% #options.each do |op| %>
<div class="checkbox">
<%= f.check_box(?????, {:multiple => true}, op, nil) %>
</div>
<% end %>
<%= f.submit class: 'btn btn-default' %>
<% end %>
This is obviously not working and I'm pretty sure this is not the right way to achieve what I want to do, so any help would be appreciated.
You could use fields_for:
<%= form_for(#account, url: options_path) do |f| %>
<%= fields_for :options do |options_form| %>
<% #options.each do |option| %>
<div class='checkbox'>
<%= options_form.label option do %>
<%= options_form.check_box option %> <%= option %>
<% end %>
</div>
<% end %>
<% end %>
<%= f.submit class: 'btn btn-default' %>
<% end %>
And in your params, you will get values like: params[:account][:options]['Op1'] with value '1' for true and '0' for false.
I am trying to pass multiple values to a custom function that I created through the check_box_tag, however I don't really know how to do it, I have check online for hours but didn't help.
Basically I have a details view, and I try to pass the date and id information of the detail to the controller and call the create method.
<%= form_tag( { :action => 'create' } ) do %>
<% #details.each do |detail| %>
<%= check_box_tag 'date[]', detail.date, false, :id => detail.id %>
<%= detail.date %>
<% end %>
<%= submit_tag 'Register!' %>
<% end %>
I try to set the custom value but when I type params in the debugger this is what it shows
{"utf8"=>"✓", "authenticity_token"=>"3PKBBKNmXyAfdSllTWBFP8EafhbrJ8rCgOeOp2NbeBA=", "date"=>["2013-06-08"], "commit"=>"Register!", "action"=>"create", "controller"=>"line_items"}
I really don't know how should I do it.
Thank you for your answer in advance!
please using array dates.
<%= form_tag( { :action => 'create' } ) do %>
<% #details.each do |detail| %>
<%= check_box_tag 'detail[dates][]', detail.date, false, :id => detail.id %>
<%= detail.date %>
<% end %>
<%= submit_tag 'Register!' %>
<% end %>
I need to have one check_box for several purposes.
For example: I have a list of files. User can choose some of them to be deleted or analysed.
I have the following code but it accepts only one submit_tag "Delete selected".
<% if #files%>
<%= form_tag destroy_multiple_files_path, method: :delete do %>
<%= submit_tag "Delete selected" %>
<% #files.each do |file| %>
<% if (arraydb.file=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
<div class="my_profile_info">
<p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
<td class="Info">
Info
</td>
</div>
<% end %>
<%end%>
<%end%>
<%else%>
<%end%>
I would like to have submit_tag "Analyse" as well.
I tried something like this but of course it did not work.
<% if #files%>
<%= form_tag destroy_multiple_files_path,analyse_multiple_files_path method: :delete,method:post do %>
<%= submit_tag "Delete selected" %>
<%= submit_tag "Analyse" %>
<% #files.each do |file| %>
<% if (arraydb.file=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
....
routes.rb:
resources :files do
collection do
delete 'destroy_multiple'
end
end
controller:
def destroy_multiple
#files = File.find(params[:files])
#files.each do |item|
item.destroy
end
end
Thanks in advance.
You can indeed have multiple submit buttons, you just have to give them names:
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
You can then check what the commit param contains in the controller and act accordingly:
if params[:commit] == 'delete'
# delete things
elsif params[:commit] == 'analyse'
# analyse things
end
The rest of the form will be submitted as usual.
this worked for me:
<%= form_tag destroy_multiple_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
controller:
if params[:commit] == 'Delete selected'
# delete things
elsif params[:commit] == 'Analyse'
# analyse things
end
routes.rb:
resources :files do
collection do
get :destroy_multiple
end
end
I have a form named purchase_products.html.erb
<%= form_for (:purchase) do |f| %>
<% for product in #purchase %>
<%= check_box_tag "product_ids[]", product.id %>
<%= product.product_name %> |
<%= product.description %> |
<%= product.link %> |
<%= link_to 'Show', product_path(product) %><br/>
<% end %>
<%= f.submit "Purchase" %>
<%= link_to 'Home', :start %>
<% end -%>
Here when I click the submit button I want to send all selected checkboxes values to a action 'my_purchase' in purchase controller.
How do i redirect my button to call my user defined method?
and how can I do to retrieve which checkboxes are selected?
Add url params to form_for. See form_for API
<%= form_for(:purchase, :url => {:action => :user_defined_method}) do |f| %>
You can access the checkboxes as an array of product ids in the method as params[:product_ids].