Rails - How to get total of params - ruby-on-rails-5

I have a Restaurant model.
Meeting model has_many :vouchers (integer)
Let's say I have 10 vouchers left, when I spent all vouchers until 0, it can be topped up to 10 more vouchers by admin (manually using restaurant form).
In Restaurant form:
<% if current_user.admin? %>
<%= form_for #restaurant %>
<%= f.number_field :voucher %>
<%= f.submit, value: 'Top Up Voucher' %>
<% end %>
<% end %>
In user show:
You have <%= #user.voucher %> vouchers left
My question is how to get total of voucher?
Ex: I have topped up voucher 3 times.
(3x10 vouchers) = **total 30 vouchers (this is what I want to get)**
I tried
<%= #user.voucher.size %>
and
<%= #user.voucher.count %>
but they both didn't work.
Thanks in advance for the help

Related

Buttons to Update Attributes

I have 3 models: Restaurant/User/Reservation.
Reservation belongs_to both other models (the other two are has_many, through:).
I need a user to be able to make a reservation # a restaurant. I wanted to make a button to do it:
<h1>Restaurants#index</h1>
<% #all.each do |x| %>
<%= link_to x.name %><br>
<%= x.image %><br>
<%= x.current_capacity %>/<%= x.capacity %>
<p><%= submit_tag "Reserve?" %></p>
<% end %>
How can I design this functionality? I was originally thinking to have a column under Restaurants for "capacity" and "current_capacity" and worry about using time later. How can I get this button to set an instance of Restaurant to update its current_capacity?

How do I create a new ul when crime name equals to 10

Count Crimes
if the list of crimes is equal to 10 on 11 create a new list, This is the updated version of it sorry....
<ul>
<% category.crimeheaders.each do |crimeheader| %>
<% crimeheader.crimes.each do |crime| %>
<li>
<%= crime.id %>
</li>
<li>
<%= crime.name %>
</li>
<% end %>
<% end
%>
How would i do this???
Ok, first you need to get the list of all crimes, regardless and not structured by crimeheader.
One way to do this is to add a relation to crimes at the category level, through the crimeheaders relation:
class Category
has_many :crimeheaders
has_many :crimes, through: :crimeheaders # this is what you add
...
end
Once you have this, you can loop through all crimes in a category, in batches of 10.
<% category.crimes.each_slice(10) do | batch | %>
<ul>
<% batch.each do |crime| %>
<li><%= crime.name %></li>
<% end %>
</ul>
<% end %>
That will get all the crimes for the category, then give you slices of 10 at a time to display in lists.
Now, if you want to get clever, you can actually retrieve from the db in batches of 10.
Probably not worth it, but here is the idea:
<% Crime.where(category_id: 123).find_in_batches( batch_size: 10 ) do | batch | %>
<ul>
<% batch.each do |crime| %>
<li><%= crime.name %></li>
<% end %>
</ul>
<% end %>

Rails 3 - Ransack - check_box_tag

Listing Model - belongs_to :area
Area Model - has_many :listings
I'm trying to implement a search using Ransack with check boxes; where user checks selected areas, search returns all the listings of the areas selected.
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
Console output:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
Just not sure how to implement it to accept multiple check box values.
Instead of using "area_id_eq", use "area_id_any". You'll also want to check to make sure that your parameters are selected:
<%= search_form_for #search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>

How to select only checked records using check_box_tag?

Guys my check_box_tag looks like as follows
<%= form_tag({:action => 'update_survey_list_status',:projectid=>params[:id], :status=>4}, :id => 'to_be_approved_frm') do %>
<% #publishedlist.each do |b| %>
<%= fields_for "beneficiaryloan[#{b.id}]" do |bloan| %>
<%= bloan.text_field :amount, :class=>'forms_txtbx'%>
<%= bloan.text_field :rate, :class=>'forms_txtbx'%>
<%= bloan.text_field :period, :class=>'forms_txtbx'%>
<% end %>
<%= check_box_tag "benificiary_id[#{b.id}]",b.id,:name => "benificiary_id[]"%>
<% end %>
<%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
And in controller, I'm reading all the beneficiary ids like this
params[:beneficiaryloan].each do |key, value|
beneficiary = Beneficiary.find(key) rescue nil
#benefciary_loan=beneficiary.beneficiaryloans.build(value)
#benefciary_loan.beneficiary_id=beneficiary.id
#benefciary_loan.hfi_id=session[:id].to_s
#benefciary_loan.status_id=params[:status]
#benefciary_loan.save if beneficiary
end
What I need is, Inserting all the beneficiary ids to [beneficiaryloans] table which are checked, but in my case it inserting all records even some of them are unchecked.
How to do I select only checked ids?
Try changing your check_box_tag to
<%= check_box_tag "beneficiaryloan[#{b.id}][enabled]", 1, true %>
Then in your controller do the following:
params[:beneficiaryloan].select{|k,v| v.delete(:enabled).to_i > 0 }.each do |k,v|
..
end
Since the enabled attribute has no influence in the model you can just delete it out of the resulting beneficiary_load hashes.

Rails 3 form only returning last element

I'm trying to build a form that will list all users and allow you to check the ones that you want to add to a team. Here's my first cut at the form:
<div id="add_team_mates">
<%= form_tag do %>
<%= will_paginate #users %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user, :size => 30 %>
<%= link_to user.name, user %>
<%= check_box_tag("add", user.id) %>
</li>
<% end %>
</ul>
<%= submit_tag "Add Team Mates", :action => "add_team_mates" %>
<% end %>
</div>
And, right now this is all that I have in the controller:
def add_team_mates
end
The problem is that if I check multiple users, I only get the last user.id rather than multiple is as I'd expect. Here's some example from the log:
Started POST "/teams/5" for 127.0.0.1 at 2011-04-14 15:28:13 -0700
Processing by TeamsController#add_team_mates as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IHFDevfKES8NibbCMlRa1t9qHn4/ZMKalK1Kjczh2gM=", "add"=>"3", "commit"=>"Add Team Mates", "id"=>"5"}
Completed in 12ms
Any help on this would be greatly appreciated. Thanks!
All your checkboxes have the same name, change the line to
check_box_tag("add[]",user.id)
In the controller your parameters will be like so:
params[:add] = ['foo','bar','baz']