Rails checkbox to get json value of selected records - ruby-on-rails

Hi I have a form in rails and this is the code
<%= form_tag getjson_products_path do %>
<% #products.each do |product| %>
<%= check_box_tag "product_ids[]",product.id , false%>
<%= product.name %>
<%= product.category %>
<%= product.price %>
<%= link_to 'Show', product %>
<%= link_to 'Edit', edit_product_path(product) %>
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
<br>
<% end %>
<%= submit_tag "submit" %>
<br>
<% end %>
and I have written one method in products controller
def getjson
#stuff to do
redirect_to root_path
end
and this is my routes file
resources :products do
collection do
get 'getjson'
end
end
I want is to get json value of selected products , but whenever I click submit it says routes error what I have to do and how to get json value for selected records?

In controller change like this
def getjson
if params[:product_ids]
#products = Product.find(params[:product_ids])
render json: #products
end
end
thats all it will works

Related

Rails: How to pass array from view to controller with button_to

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

Combining Multiple Models into one view Rails

I want to show data from 4 models into a single view. I have created a separate controller for this named posts_controller.rb and in views I have created posts folder and created index.html.erb file.
After that in controllers file I have added the following code.
class PostsController < ApplicationController
def index
#quotes = Quote.all.order("created_at DESC")
#images = Image.all.order("created_at DESC")
#jokes = Joke.all.order("created_at DESC")
#items = (#quotes.to_a + #jokes.to_a)
end
end
And here is the view file where I am trying to show 2 items data as for now. But its not working. Pls check the code.
<% if #items.any? %>
<div class="col-md-12">
<% #items.each.do |item| %>
<% if item.is_a? Quote %>
<div class="postbg">
<%= quote.quotefie %>
<div class="wedate pull-right wehi">
<%= quote.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<em class="pull-right wehi" style="margin-top:20px;"> - <%= quote.author %></em>
<%= link_to 'Show', quote %>
<%= link_to 'Edit', edit_quote_path(quote) %>
<%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% else %>
<% #jokes.each do |joke| %>
<div class="postbg">
<%= joke.jokefie %>
<div class="wedate pull-right wehi">
<%= joke.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<%= link_to 'Show', joke %>
<%= link_to 'Edit', edit_joke_path(joke) %>
<%= link_to 'Destroy', joke, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<% end %>
</div>
<% end %>
Here is the error it is showing.
undefined method `do' for #<Enumerator:0x007fd55fb032b8>
Your problem is in this line here:
<% #items.each.do |item| %>
do defines a block, not a method call, so it should be:
<% #items.each do |item| %>
Note the removed ..
You've used do correctly further down the view when you use #jokes.each do |joke|.
This line
<% #items.each.do |item| %>
should be
<% #items.each do |item| %>

Ruby on Rails multiple Buttons

I have the following written:
<% if #document != nil %>
<%= form_for(#document, :html => { :multipart => true }) do |f| %>
<% if #document.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#document.errors.count, "error") %> prohibited this document from being saved:</h2>
<ul>
<% #document.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<%= render partial: 'documents/resume' %>
<div id="add_buttons">
<%= f.file_field :resume %>
<%= f.submit %>
</div>
<% end %>
<% else %>
<h1>No Document Available</h1>
<% end %>
Sorry about the indenting, copy/paste didn't paste it right. Anyways, this is using paperclip and works fine to upload a document as long as in the resume partial that I have listed I have my delete button commented out. Here is the code for that partial:
<div class='Resume_1'>
<%#= #current_user = session[:user] %>
<%#= #document = #current_user[:document] %>
Current Resume:
<%= render partial: 'documents/resumelink' %>
</div>
<div class='Resume_2'>
<% if (#document != nil) && (#document.resume_file_name != nil) %>
<%= button_to "Delete Resume", #document, method: :delete, data: { confirm: 'Are you sure you wish to delete your resume?' } %>
<% end %>
</div>
If the button_to line is commented out, the update document (f.submit) button works fine. However, if the button_to line is left in, the f.submit button does nothing. And here's the destroy function (which is where the delete method is routed to) since this doesn't actually seem to remove the resume either.
def destroy
#document.resume = nil
respond_to do |format|
format.html { redirect_to welcome_url, notice: 'Resume was successfully deleted.' }
format.json { head :no_content }
end
end
Thanks for any help.
If you look at docs. The html generated by button_to helper is
<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
# <div><input value="New" type="submit" /></div>
# </form>"
So when you have a button_to inside a form you basically have a form inside a form and hence your submit button doesn't work.
Fix:
Instead of using a button_to use link_to helper and give bootstrap classes or some custom class to make it look like a button
<%= link_to "Delete Resume", #document, class: "btn btn-default" method: :delete, data: { confirm: 'Are you sure you wish to delete your resume?' } %>

Ruby on Rails: one check_box for several submit_tag

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

How to params[] value to a user-defined action

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].

Resources