I have a question with this code. In this view I iterate over an array but I want to have the option to delete an entry from the array. Here is my code
view
<table>
<tr>
<th>Ware</th>
<th>Anzahl</th>
<th>Einzelpreis</th>
<th>Gesamtpreis</th>
</tr>
<% counter = 0 %>
<% $itemarray.each do |iarray| %>
<tr>
<% #items.each do |item| %>
<% if iarray.to_i == item.id %>
<th> <%= item.name %> </th>
<th> <%= $anzahlarray[counter] %> </th>
<th> <%= item.price.round(2) %> € </th>
<% preistemp = preistemp = item.price * $anzahlarray[counter].to_f %>
<th> <%= preistemp.round(2) %> € </th>
<th> <%= link_to 'Show', item_path(item) %> </th>
<th> <%= link_to 'Destroy', :hidden_param => 'counter' , method: :deleteshoppingcartentry(counter), data: {confirm: 'Are you sure?' }%></th>
<% end %>
<% end %>
</tr>
<% counter += 1 %>
<% end %>
</table>
controller
def deleteshoppingcartentry
$itemarray.delete_at($entrynumber)
$anzahlarray.delete_at($entrynumber)
redirect_to "/orders/new"
end
maybe you could help me out
You can't delete items from a global array variable. Use a database instead. Learn more about Rails' ActiveRecord.
Related
attendance table that takes has attribute status, date,recuritment_id, and project_site_id .
project_site has one_to_many association with attendance
recuritmenthas one_to_many association with attendance.
i am taking attribute from recruitment table when attribute status is joined in attendance#form view.
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
here #project_site.attendance_month contains the attendance month value. based on month i calculate number of days column along with name from recuritmnet table.
here is view-
holydays master contains corresponding month holyday's date and in view it auto match date and prints "H",
All input default selected as P. there is final submission_button that chnages boolean attribute. now on final submission i want to push all default selected P into attendance table.
attendance_controller.rb
def new
if #project_site.submission_status == false
#attendance = Attendance.new
#date = HolydayCalendar.all
#recruitment = Recruitment.all.where(current_status: "2")
else
redirect_to project_site_attendances_path
end
end
from.html.erb (attendance controller view)
<table>
<thead>
<tr>
<th class="attendance-emp-name">Emp. Name</th>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<th class="text-center"><%= date %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #recruitment.where(location: #project_site.site_id).each do |recruitment| %>
<tr>
<td class="attendance-emp-name"><%= recruitment.name %></td>
<% (1..(Time.days_in_month #project_site.attendance_month.strftime("%m").to_i)).each do |date| %>
<%= form_with(model: attendance, :html => {:id => 'attendance-form-validation'}, url:[#project_site, #attendance], local: true) do |f| %>
<% if HolydayCalendar.find_by(date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s), total_site_id: #project_site.site_id)%>
<td class="holyday text-center"><%= "H" %></td>
<% elsif recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) == nil %>
<td>
<%= f.select :status, [['P', 1], ['A', 2], ['L', 4], ['WE', 5], ['CO', 6]], {}, { onchange: 'this.form.submit()', class: 'attendance-select-input' } %>
</td>
<% else %>
<% attendance_value = recruitment.attendances.find_by(attendance_date: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)) %>
<%if attendance_value.status == 1 %>
<td class="presant text-center"><%="P" %></td>
<% elsif attendance_value.status == 2 %>
<td class="absent text-center"><%="A" %></td>
<%elsif attendance_value.status == 3 %>
<td class="holyday text-center"><%="H" %></td>
<%elsif attendance_value.status == 4 %>
<td class="leave text-center"><%= "L" %></td>
<%elsif attendance_value.status == 5 %>
<td class="weekend text-center"><%= "WE" %></td>
<%elsif attendance_value.status == 6 %>
<td class="compoff text-center"><%= "CO" %></td>
<% end %>
<% end %>
<%= f.hidden_field :attendance_date, value: (#project_site.attendance_month.strftime("%Y-%m")+"-"+date.to_s)%>
<%=f.hidden_field :recruitment_id, value: recruitment.id%>
<%=f.hidden_field :project_site_id, value: #project_site.id%>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% if #project_site.submission_status == true %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary disabled" %>
</div>
<% else %>
<div class="text-center">
<%= link_to "Submit Attendance", set_submission_status_project_site_path(#project_site), method: :put, data: { confirm: 'Make Sure you marked all attendance before submission' }, :class=>"button primary" %>
</div>
<% end %>
project_sites_controller.rb
def set_submission_status
#project_site = ProjectSite.find(params[:id])
#project_site.update(submission_status: true)
end
It's a little unclear what your question is, but I gather it has to do with your form sending back an array op options.
If you want your html form to return an array of information, then you need to indicate it in your html using the name = "attendance[]" syntax, notice the square brackets.
Check out this article on the subject https://mattstauffer.com/blog/a-little-trick-for-grouping-fields-in-an-html-form/
I'm using kaminari gem for pagination in my rails application to paginate the categories.
I have implemented all the process, and the pagination links are also being shown. But this is not effecting the table elements list.
Index action in controller
def index
#categories = current_user.categories
#categories = Kaminari.paginate_array(#categories).page(params[:page]).per(5)
end
Index view
<h1>Categories</h1>
<div class="well">
<%= link_to "+ New Category", new_category_path, class: "btn btn-success" %>
</div>
<%= paginate #categories %>
</br>
<%= page_entries_info #categories %>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% if current_user.categories.count == 0 %>
<tr>
<td colspan="2">
No categories found. Click the button above to add a new one.
</td>
</tr>
<% else %>
<% current_user.categories.each do |category| %>
<tr>
<td><%= link_to category.name, category %></td>
<td><%= link_to "Edit", edit_category_path(category), class: "btn btn-primary" %>
<%= link_to "Delete", category, method: :delete,
data: { confirm: "You sure?" }, class: 'btn btn-small btn-danger' %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Screenshot
As shown in the above picture it is showing all the 7 entries for a pagination of 5/page.
Can some one please explain the reason why it is not working.
I think issue is with your array type. Please try following code.
def index
#categories = current_user.categories.all
unless #categories.kind_of?(Array)
#categories = #categories.page(params[:page]).per(5)
else
#categories = Kaminari.paginate_array(#categories).page(params[:page]).per(5)
end
end
Have you tried this way? Its more optimised, only fetch those records that required.
#categories = current_user.categories.page(params[:page]).per(5)
Solution adopted from:
rails 3,Kaminari pagination for an simple Array
This is the controller I have writter for Orders.
def index
if current_user.has_role? :admin
#orders = Order.all.paginate(:page => params[:page], :per_page => 5)
elsif current_user.has_role? :customer
#orders = Order.where(:email => current_user.email).paginate(:page => params[:page], :per_page => 5)
elsif current_user.has_role? :white_label
#orders = Order.where(:performer_id => (Performer.where(:white_label_id => current_user.white_label.id))).paginate(:page => params[:page], :per_page => 5)
else
#orders = current_user.performer.orders.paginate(:page => params[:page], :per_page => 5)
end
#custom_video = CustomVideo.new
end
As you see I have used will_paginate gem to do pagination. This is the view page where I am doing pagination.
<div class="container">
<div class="span 8">
<% if current_user.has_role? :admin %>
<%role =1%>
<%elsif current_user.has_role? :performer %>
<%role =2%>
<% else %>
<%role =3%>
<% end %>
<h3>Awaiting Upload</h3>
<table id="order_table" class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th>Description</th>
<th>Total</th>
<% if role==2 %>
<th>Select a video to upload for the order</th>
<%end %>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if !CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date + var1.days%>
<tr>
<td> <%= order.id%></td>
<td><% if order.location %>
<%= order.location.name %>
<% end %></td>
<td><% if order.performer %>
<%= order.performer.first_name %>
<% end %></td>
<td><% if order.duration %>
<%= order.duration.time %>
<% end %></td>
<td><% if order.quality %>
<%= order.quality.name %>
<% end %></td>
<td><% if order.delivery_time %>
<%= order.delivery_time.duration %>
<% end %></td>
<td><% if order.clip_category %>
<%= order.clip_category.name %>
<% end %></td>
<td><% if order.description %>
<%= order.description %>
<% end %></td>
<td><%= order.total %></td>
<% if role==2 %>
<td>
<%= simple_form_for(#custom_video, :html => { :multipart => true, :id => "fileupload" }) do |f| %>
<%= f.error_notification %>
<%= f.file_field :path%><br/><br>
<%= f.hidden_field :order_id,:value => order.id %>
<%=f.button :submit, :value=>"Save", :class=>"btn btn-success" %>
<% end %>
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%"></div></div>
</div>
</script>
<%end%>
</td>
<td>
<% if role==1 %>
<%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
<%= hidden_field_tag(:id, order.id) %>
<%= submit_tag ("Refund"),:class => "btn btn-success download" %>
<% end %>
<% end %>
</td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
<%else %>
<% next %>
<%end %>
<% end -%>
<% end %>
<%= will_paginate #orders, :param_name => 'awaiting_orders' %>
</tbody>
</table><br/>
<h3>Completed Orders</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th>id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if CustomVideo.find_by_order_id(order.id) and Time.now <= order.created_at.to_date+var1.days%>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<td>
<% if role==1 %>
<%=form_tag({controller: "orders", action: "refund"}, method: "post") do%>
<%= hidden_field_tag(:id, order.id) %>
<%= submit_tag ("Refund"),:class => "btn btn-success download" %>
<% end %>
<% end %>
</td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table><br/>
<h3>Expired Orders</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th> id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.delivery_time_id=1%>
<% var1=14 %>
<% else %>
<% var1=7 %>
<% end %>
<% if Time.now > order.created_at.to_date+var1.days%>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :update, #order %>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table>
<br>
<h3>Orders Refunded</h3>
<table class="table table-hover" id="order_table">
<thead>
<tr>
<th> id</th>
<th>Location</th>
<th>Performer</th>
<th>Duration</th>
<th>Quality</th>
<th>Delivery</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #orders.each do |order| %>
<% if order.refunded %>
<tr>
<td> <%= order.id%></td>
<td><%= order.location.name %></td>
<td><%= order.performer.first_name %></td>
<td><%= order.duration.time %></td>
<td><%= order.quality.name %></td>
<td><%= order.delivery_time.duration %></td>
<td><%= order.clip_category.name %></td>
<% if can? :show, #order %>
<td><%= link_to 'Show', order %></td>
<% end %>
<% if can? :destroy, #order %>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<%else %>
<% next %>
<%end %>
<% end -%>
</tr>
<% end %>
<%= will_paginate #orders %>
</tbody>
</table>
<br>
</div>
</div>
<%# link_to 'New Order', new_order_path %>
The problem in this is that. If I click the second page for the first table, it is going to the second for the next table too. I have tried giving the :param_name => 'questions_page' suggested here. But the trouble here is that, I am not using different instance variable for the tables but the same one. How do I solve this?>
You've almost answered your own question. You need to be using separate instance variables for each table if you want them to behave separately.
I have the following view:
<table class="fixed">
<tr>
<th>Student Name</th>
<!-- create as many <th> as there are evaluations -->
<% #eval_count.times do |i| %>
<th>Evaluation <%= i+1 %></th>
<% end %>
<th>Student Average <br />(for this goal)</th>
</tr>
<% for eval in #evals %>
<tr class="<%= cycle("odd", "even", name: "evals")%>">
<!-- eval returns { s_id [eval],[eval]} -->
<td><%= eval[1].first.student.name%></td>
<!-- in each student's row, print the score for each consecutive evaluation -->
<% #eval_count.times do |i| %>
<td><%= eval[1][i].score %><% #ss_scores << eval[1][i].score %></td>
<% end %>
<td><%= #ss_scores %></td>
</tr>
<% reset_cycle("evals") %>
<% end %>
</table>
<% #ss_scores.in_groups(#student_count, false) do |group|%>
<%= (group.sum.to_f/group.size).round(2) %>
<% end %>
which renders the following:
I want to put the average for each student in the last column, but #ss_scores is a variable and so calling anything on it doesn't work. But when the for loop has finished, #ss_scores can be worked with nicely as in the bottom of the screenshot. Any idea how to do this better?
Try emptying the array everytime, using [] and calculate the average inline, like below
<td><%= #ss_scores.inject(0.0) { |sum, el| sum + el } / #ss_scores.size %></td>
<% #ss_scores = [] %>
-
<% for eval in #evals %>
<tr class="<%= cycle("odd", "even", name: "evals")%>">
<!-- eval returns { s_id [eval],[eval]} -->
<td><%= eval[1].first.student.name%></td>
<!-- in each student's row, print the score for each consecutive evaluation -->
<% #eval_count.times do |i| %>
<td><%= eval[1][i].score %>
<% #ss_scores << eval[1][i].score %>
</td>
<% end %>
<td><%= #ss_scores.inject(0.0) { |sum, el| sum + el } / #ss_scores.size %></td>
<% #ss_scores = [] %>
</tr>
<% reset_cycle("evals") %>
<% end %>
I've setup a Rails project:
User->Project->Sample
When I view my Projects as table everything is fine. I render a table template:
<%= render 'layouts/projects_table' %>
but when I render my samples for a project
<%= render 'layouts/samples_table' %>
I get the table but before the table I get my raw data rendered:
[#<Sample id: 28, name: "abcd", size: 11, quantity: 11.0, created_at: "2013-04-04 09:58:50"> ... ]
ProjectsController:
def show
#project = Project.find(params[:id])
#samples = #project.samples
end
_samples_table:
<table id="samples" class="display">
<thead>
<tr>
<th>
Sample Name
</th>
<th>
Size
</th>
<th>
Quantity
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<%= #samples.each do |sample| %>
<tr>
<td>
<%= link_to sample.name, project_sample_path(#project, sample) %>
</td>
<td>
<%= sample.size %>
</td>
<td>
<%= sample.quantity %>
</td>
<td>
<% if !sample.libraries.any?%>
<%= link_to 'Del', project_sample_path(#project, sample),
:confirm => 'Are you sure?', :method => :delete %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Everything else works fine.
Any help would be appreciated!
Oliver
Remove the = from the loop definition
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>
You're outputting the return value of .each.
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>