I am trying to create a spreadsheet on index page to display items with checkbox and if validated - the checkbox will be disabled so that people won't mis-ticked it.I was wondering should i do it with controller or the view? thanks : can you help?
In app/views/scooties_coupons/index.html.erb
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>
in app/controllers/scotties_coupons_controller.rb:
def set_valid_coupons
to_valid = params[:scooties_coupons].select do |id, attrs|
attrs[:validated] == '1'
end
to_not_valid = params[:scooties_coupons].reject do |id, attrs|
attrs[:validated] == '1'
end
ScootiesCoupon.transaction do
ScootiesCoupon.where(id: to_valid.keys, validated: false).update_all(
validated:true)
ScootiesCoupon.where(id: to_not_valid.keys, validated: true).update_all(
validated:false)
end
redirect_to action: :index, notice: 'Validations updated'
end
you can do something like
<table class="table table-hover">
<h1>Scooties Coupons</h1>
<%= form_with(url: validate_coupons_path, method: 'patch') do |f| %>
<table>
<thead>
<tr>
<th>Valid</th>
<th>Coupon</th>
<th>Redeemed</th>
<th>First name</th>
<th>Surname</th>
<th>email</th>
<th>occupation</th>
<th>validation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #scooties_coupons.each do |scooties_coupon| %>
<tr>
<td>
<% if true %>
SOMETHING HERE
<% else %>
<%= fields_for('scooties_coupons[]', scooties_coupon) do |cf|
cf.check_box(:validated)
end %>
<% end %>
</td>
<td><%= scooties_coupon.coupon %></td>
<td><%= scooties_coupon.redeemed %></td>
<td><%= scooties_coupon.first_name %></td>
<td><%= scooties_coupon.surname %></td>
<td><%= scooties_coupon.email %></td>
<td><%= scooties_coupon.occupation %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit %>
<% end %>
<br>
</table>
Related
I have this table
t.string "first_name"
t.string "last_name"
t.integer "age"
t.string "email"
I am using some search functionality where I search for a Barber by first_name .
Then I am displaying it and whatever else is similar to the input.
This is my current view (search.html.erb)
<h1>searches</h1>
<%= #barbers.each do |barber|%>
<%= barber.first_name%><br>
<%end%>
Very plain and simple and this is how it looks.
I obviously don't want to keep it like this.
I want it to look like this , the way I have it in my index.html.erb
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="row">First name</th>
<th scope="row">Last name</th>
<th scope="row">Age</th>
<th scope="row">Email</th>
<th scope="row">User ID</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #barbers.each do |barber| %>
<tr>
<td><%= barber.first_name %></td>
<td><%= barber.last_name %></td>
<td><%= barber.age %></td>
<td><%= barber.email %></td>
<td><%= barber.user_id %></td>
<td><%= link_to 'Show', barber %></td>
<td><%= link_to 'Edit', edit_barber_path(barber) %></td>
<td><%= link_to 'Destroy', barber, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
I am just confused on how to write the code since I am searching for each. #barbers.each do |barber|
Here is what I done...
<h1>searches</h1>
<table class="table table-dark table-striped">
<thead>
<tr>
<th scope="row">First name</th>
<th scope="row">Last name</th>
<th scope="row">Age</th>
<th scope="row">Email</th>
<th scope="row">User ID</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #barbers.each do |barber| %>
<tr>
<td><%= barber.first_name %></td>
<td><%= barber.last_name %></td>
<td><%= barber.age %></td>
<td><%= barber.email %></td>
<td><%= barber.user_id %></td>
</tr>
<% end %>
</tbody>
</table>
I have used Ransack Gem,Rails 4.
roles_controllers.rb
def index
#q = Role.ransack(params[:q])
#roles = #q.result(distinct: true)
end
This is instance method in model(role.rb)
def user_count
self.users.count
end
This is my html table header(Roles/index.html.erb)
<table class="table table-bordered table-framed rb-usr-tbl">
<thead>
<tr>
<th><%= sort_link(#q, :name) %></th>
<th>Description</th>
<th><%= sort_link(#q,:role_users_user_count) %></th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody class="role-index">
<% #roles.each do |role| %>
<tr>
<td><%= role.human_role %></td>
<td><%= role.description %></td>
<td><%= role.user_count %></td>
<td>
<%= link_to edit_role_path(role), :remote => true,:title => "Edit Role" do %>
<i class="icon-pencil7 rb-pencil"></i>
<% end %>
</td>
<td>
<%= link_to role, method: :delete,:title => "Delete Role", data: { confirm: 'Are you sure?' } do %>
<i class="icon-trash rb-trash"></i>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
There is relationship between user and role are One role has_many users.
Here i want to sort this count of users in asc and desc order but with this code it does not work.
Help me if you can.
Thanks
Ransack is very smart itself, try to change line
<th><%= sort_link(#q,:role_users_user_count) %></th>
to
<th><%= sort_link(#q, :users_count) %></th>
Explanation: In the variable #q you keep a Ransack query object on the model Role, if you call sort_link on #q it will apply users_count as "join users relation and order by users count".
I know there are alot of questions on this, but after looking at about a dozen or so none of them appear to be the same problem. I have an "Action" model that I am just simply trying to edit and update. (fyi, this isn't a nested form, I'm not using devise, and this isn't an activeadmin problem...like virtually all other questions about this topic are). I'm getting
error:
unpermitted parameters: utf8, _method, authenticity_token when I do this.
Action params in Actions_controller:
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
IF I change it to:
params.require(:action).permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
than I get the below error (and I can't find any solution for this):
undefined method `permit' for "update":String
Actions Controller:
def edit
#action = Action.find(params[:id])
#actiontypes = Actiontype.all
#categories = Category.all
#lead_categories = #categories.where(lead: true)
#user = current_user
#reviewer = User.find(#action.reviewer_id) if #action.reviewed?
end
def update
#action = Action.find(params[:id])
if #action.update!(action_params)
binding.pry
flash[:success] = "Loop closed!"
redirect_to '/closingloop/actions?reviewed=false'
else
flash[:danger] = "Something Went Wrong! The loop was not closed!"
render :edit
end
end
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
View:
<%= form_for [:closingloop, #action] do |f| %>
<%= f.hidden_field :reviewer_id, :value => #user.id %>
<%= f.hidden_field :reviewed, :value => true %>
<table width="70%" align="center" class="table table-responsive">
<tr>
<th>Tracking Number</th>
<td><%= #action.company.tracking_phone_number %></td>
</tr>
<tr>
<th>Target Number</th>
<td><%= #action.company.phonenumber %></td>
</tr>
<tr>
<th>Opportunity Name</th>
<td><%= #action.opportunity_name %></td>
</tr>
<tr>
<th>Opportunity Address</th>
<td><%= #action.customer_address %></td>
</tr>
<tr>
<th>Opportunity Phone</th>
<td><%= #action.caller_phone_number %></td>
</tr>
<tr>
<th>Call Recording</th>
<td><%= audio_tag(#action.call_recording_link, controls: true) %></td>
</tr>
<tr>
<th>Duration</th>
<td><%= #action.duration %></td>
</tr>
<tr>
<th>Call Status</th>
<td><%= #action.call_status %></td>
</tr>
<tr>
<th>Date & Time</th>
<td><%= #action.created_at.to_formatted_s(:long) %></td>
</tr>
<% if #action.reviewed? %>
<tr id="row_reviewed_by">
<th>Reviewed By</th>
<td><%= #reviewer.first_name %> <%= #reviewer.last_name %></td>
</tr>
<% end %>
<tr><td colspan="2"> </td></tr>
<tr id="q_call_answered">
<th>Call Answered?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :call_answered, true, class: 'call_answered_true' %>
<%= f.label :call_answered, "Yes" %>
<%= f.radio_button :call_answered, false, class: 'call_answered_false' %>
<%= f.label :call_answered, "No" %>
</div>
</td>
</tr>
<tr id="why_not_answered" style="display:none;">
<th>Why wasn't it answered?</th>
<td>
<%= f.select :why, options_for_select([["No Answer", "N"], ["Abandoned", "A"], ["After Business Hours", "H"]], #action.why), { include_blank: true } %>
</td>
</tr>
<tr id="q_opportunity" style="display:none;">
<th>Was it a opportunity?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :is_customer, true, class: 'opportunity_true' %>
<%= f.label :is_customer, "Yes" %>
<%= f.radio_button :is_customer, false, class: 'opportunity_false' %>
<%= f.label :is_customer, "No" %>
</div>
</td>
</tr>
<tr id="opportunity_type" style="display:none;">
<th>Opportunity Type</th>
<td>
<%= f.select :actiontype_id, options_from_collection_for_select(#actiontypes, 'id', 'action_type', #action.actiontype_id), { include_blank: true } %>
</td>
</tr>
<tr id="reason_for_call" style="display:none;">
<th>Reason for Call</th>
<td><%= f.select :category_id, options_from_collection_for_select(#categories, 'id', 'reason', #action.category_id), { include_blank: true } %></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" id="save" class="btn btn-success" /></td>
</tr>
</table>
<% end %>
The main problem here is that if you have model named 'action', your params by default will be named in the same way, which collides with ActionController default params. The simplest solution is, I guess, renaming your model.
In my costs table I have created a variable to calculate the rate multiplied by the hours and I can obtain my figure successfully. However, I need to calculate the total of each row at the end of my table. One of my fields are "total of all rows". What is the best course of action for this?
Any suggestions?
In my costs/index.html.erb file this is the code I currently have
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<td><%= cost.total_of_all_rows %></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
I have fixed my code yet I am still not getting the desired result my total_of_all_rows is not adding up the sum of rate and quantity. My amended code is:
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
<tr>
<td><%= cost.total_of_all_rows %><%=Cost.sum(:total) %></td>
</tr>
<% end %>
<tbody>
</tbody>
</table>
My migration for this table is
class CreateCosts < ActiveRecord::Migration
def change
create_table :costs do |t|
t.string :mini_description
t.string :description
t.string :quantity
t.string :rate
t.string :total
t.string :total_of_all_rows
t.references :job, index: true
t.timestamps
end
end
end
1)Don't store the total_of_all_rows in the databse as long as it is variable and you will have to update it with each new entry.
2)Delete column from migration.
3)In your view:
<tbody>
<% #costs.each do |cost| %>
<tr>
<td><%= cost.mini_description %></td>
<td><%= cost.description %></td>
<td><%= cost.quantity %></td>
<td><%= cost.rate %></td>
<td><%= cost.total%><%=cost.cost_var%></td>
<!--<td><%#= cost.job %></td>-->
<td><%= link_to 'Show', cost %></td>
<td><%= link_to 'Edit', edit_cost_path(cost) %></td>
<td><%= link_to 'Destroy', cost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
<thead>
<tr>
<th>Total of all rows</th>
</tr>
</thead>
<tr>
<td><%= #costs.sum(:total) %></td>
</tr>
<% end %>
<tbody>
If you still want to store the value:
class Cost < ActiveRecord::Base
after_save :update_totals
def update_totals
Item.all.each {|x| x.total_of_all_rows = Item.sum(:total)
end
end
This is my code:
<table class="video_table">
<% count = 0 %>
<tr>
<% #f_videos.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% count +=1 %>
<% if count == 4 %>
</tr>
<% end %>
<% end %>
</table>
at each 4 videos placed I want the table to switch row. So I implemented a counter.
But it is not working. Any ideas?
Your count will only be set to 4 once.
Instead of if count == 4 use if count % 4 == 0
This will repeat the </tr> for each multiple of 4
Alternatively, you could skip using the count variable and use each_with_index to get the same result
<table class="video_table">
<% #f_videos.each_with_index do |f_video, i| %>
<tr>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% if (i+1) % 4 == 0 %>
</tr>
<% end %>
<% end %>
</table>
Even better! each_slice
<table class="video_table">
<% #f_videos.each_slice(4).to_a do |slice| %>
<tr>
<% slice.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% end %>
</tr>
<% end %>
</table>
Another solution:
<table class="video_table">
<% #f_videos.in_groups_of(4) do |group| %>
<tr>
<% group.each do |f_video| %>
<td><%= f_video.name %></td>
<td><%= f_video.date_added %></td>
<td><%= f_video.views %></td>
<% end %>
</tr>
<% end %>
</table>
in_groups_of has the added value/advantage that, when needed, it allows for padding any remaining slots with eg ' ', which can be very useful. See the docs.