rails dynamic selection from list - ruby-on-rails

i need to change content depending which items is chosen. I am pretty new to rails and cant figure out how to do this. I know i need to use ajax, i guess! Okey.
Here is the list
<div class="table_wrap city_list_wrap">
<table class="city_list table normal">
<tbody>
<tr>
<th colspan="3"> City list </th>
</tr>
<% #cities.each_slice(3) do | slice | %>
<tr>
<% slice.each do |city| %>
<td class="city_tab"><%= city.title %> (<%= city.car_dealers_count %>) </td>
<% end %>
<tr>
<% end %>
</tbody>
</table>
</div>
And what after i push on one of the cities u need to show the list of that city items in table like this:
<div class="table_wrap">
<table class="table normal clickable" id="item_table">
<tbody>
<tr>
<th style="width:30%"><%= t("public.item.title") %></th>
<th style="width:45%"><%= t("public.item.info") %> </th>
<th style="width:25%"><%= t("public.item.logo") %> </th>
</tr>
<% city_title = "" %>
<% #items.each do | item | %>
<% unless city_title == item.city.title %>
<% city_title = item.city.title %>
<tr class="caption"><td colspan="3"><%= city_title %></td></tr>
<% end %>
<tr id="item-<%= item.id %>" class="hoverable">
<td><%= item.title %></td>
<td>
<p>
<%= item.address %><br><%= item.phone_number %><br/><%= link_to item.site, "http://#{item.site}", target:"_blank", rel: "nofollow", class: "item-link" if item.site.present? %>
</p>
</td>
<td><%= image_tag item.small_logo, alt: item.title %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
At the start this list is shown and all items are shown. After i click on one of the cities i need to show only items from that city in this second table.. How to do this? Please help. Thanks.

You can use this gem: https://github.com/innku/metropoli
Read the helper, it's real easy to install.

Okey i did this pretty simple way gave every city field id its id and rows with city items that id too. and after click $("id").hide() or show() depending on situation

Related

How to create a non-static HTML table header and rows

I have two arrays, and I'd like to create a table with dynamic header cells (from the first array called subjects) and iteratively add contents (from the second array called examscores) in table rows with respect to the table header value.
Desired outcome is (fiddle):
The erb code is:
<table width="100%" border="1">
<thead>
<tr>
<th rowspan="2" scope="col">NAME</th>
<th colspan="<%= #subjects_by_class.size %>" scope="col">Subjects/Scores</th>
<th rowspan="2" scope="col">Total</th>
<th rowspan="2" scope="col">Average</th>
</tr>
<tr>
<% #subjects_by_class.each do |s| %>
<th> <%= s.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% #examscore.each do |ex| %>
<tr>
<td><%= get_student_name_by_id(ex.student_id) %></td>
<% #subjects_by_class.each do |ss| %>
<% #examscore.each do |ii| %>
<% if ss.id == ex.subject_id %>
<td> <%= i.total %> </td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(ex.student_id, year_id) %> </td>
<td><%= avg_student_totalscore(ex.student_id, year_id) %></td>
</tr>
<% end %>
</tbody>
</table>
The output I get is (fiddle):
A new tr is created under Maths subject instead of a new td for Arts subject, and this results in Average td being distorted.
Any insight will be greatly appreciated.
Well, just look at this section of your code:
<% #examscore.each do |ex| %>
<tr>
You create a new line for each #examscore, and you have 4 of those (1 per user/subject, so you end up with 4 lines of course).
Your tbody should be something like this:
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #subjects_by_class.each do |subject| %>
<% #examscore.each do |score| %>
<% if score.subject_id == subject.id && score.student_id == student.id %>
<td><%= score.total %></td>
<% break %>
<% end %>
<% end %>
<% end %>
<td><%= sum_student_totalscore(student.id, year_id) %> </td>
<td><%= avg_student_totalscore(student.id, year_id) %></td>
</tr>
<% end %>
</tbody>
It's a bit strange that you only care about the year in your totals
Also you could improve things a little bit by having a method in your Student class that returns an array of scores for a given year/list of subjects
# Returns an array of scores for the given year
def scores(year, subject_ids)
subject_ids.map do |subject_id|
# find score for year & the given subject_id
end
end
This way your body would become
<tbody>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<% #scores = student.scores(year_id, #subjects_by_class) %>
<% #scores.each do |score| %>
<td><%= score.total %></td>
<% end %>
<% scores_total = #scores.sum(&:total) %>
<td><%= scores_total %> </td>
<td><%= scores_total / #scores.size.to_f %></td>
</tr>
<% end %>
</tbody>
which I found more clear, but it could be further improved with decorators for instance.

Can't get bootstrap collapse to work in Rails (can't generate a new target-data id)

I've been trying to create a basic event listing site where it will display a list of events in a table format. I was hoping to make every row have a collapse effect with bootstrap. The problem that I'm running into is that I can get the collapse working but only for one single row. Later I learned that Bootstrap identifies each target-data id differently, so I've been trying to figure out how to put a unique id into the loop. Any clue? Here's what I have so far.
<table class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th>Title</th>
<th>Link</th>
<th>start time</th>
</tr>
</thead>
<tbody>
<% #events.group_by(&:date).each do |date, events| %>
<tr><td colspan="100"><h1><%= date.strftime("%B %d") %></h1></td></tr>
<% events.each do |event| %>
</tbody>
<tr data-toggle="collapse" data-target="<%= event.id %>" class="accordion-toggle">
<td><%= event.title %></td>
<td><%= event.link %></td>
<td><%= event.start_time.strftime("%I:%M:%S %p") + "PST" %></td>
<td class="hiddenRow">
<div class="accordian-body collapse" id="<%= event.id %>">
<%= event.description%> </div>
</td>
</tr>
<% end %>
<% end %>
</table>
Solved, basically the event didn't have an id in the active record, so I had to use .each_with_index to generate an index for a new id. Hope this helps anyone that is struggling.

Rails form to update a record within an index partial

Hi can anyone tell me what I'm doing wrong here. I'm trying to allow the user update certain fields from within the index. However, when I press the button on the screen to update, nothing happens and the record in the database remains unchanged. What am I missing or doing wrong?
views/company_pays/_index.html.erb
<h1>Company Pays</h1>
<table class="table">
<thead>
<tr>
<th>Description</th>
<th>Type</th>
<th>Units</th>
<th>Apply deduction1</th>
<th>Apply deduction2</th>
<th>Apply deduction3</th>
<th>Apply deduction4</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #company_pays.each do |company_pay| %>
<tr>
<td><%= company_pay.description %></td>
<td><%= company_pay.pay_sub_head.short_desc %></td>
<td class="input"><%= form_for company_pay, :method => :patch do |f| %></td>
<% if company_pay.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(company_pay.errors.count, "error") %> prohibited this company_pay from being saved:</h2>
<ul>
<% company_pay.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<td class="input"><%= f.text_field :units %></td>
<td class="input"><%= f.check_box :apply_deduction1 %></td>
<td class="input"><%= f.check_box :apply_deduction2 %></td>
<td class="input"><%= f.check_box :apply_deduction3 %></td>
<td class="input"><%= f.check_box :apply_deduction4 %></td>
<td class="actions"><%= f.submit "Update" %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
Thanks for looking

How to display a single record from database in ruby on rails

I am very new to ruby on rails and am developing a catalogue application. I have set up my database and have added some entries. At the moment on my main page it is displaying all the data in my database but how do i go about just displaying one record for example. Here is my code:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Bann.com - slightly smaller than Amazon!</h1>
<% #products.each do |product| %>
<div class="entry">
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td><%= image_tag(product.image_url) %></td>
<td><%= image_tag(product.image_url) %></td>
</tr>
<tr>
<td><h3><%= product.title %></h3>
<div class="price_line">
<span class="price"><%= number_to_currency product.price, :unit=>'£' %></span>
</div>
</div></td>
<td>row 2, cell 2</td>
</tr>
</table>
<% end %>
Right now you're looping over everything in products in your #products.each do |product| loop. If you wanted to display just the first of these, you could:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Bann.com - slightly smaller than Amazon!</h1>
<% product = #products.first %>
<div class="entry">
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td><%= image_tag(product.image_url) %></td>
<td><%= image_tag(product.image_url) %></td>
</tr>
<tr>
<td>
<h3><%= product.title %></h3>
<div class="price_line">
<span class="price"><%= number_to_currency product.price, :unit=>'£' %></span>
</div>
</td>
<td>row 2, cell 2</td>
</tr>
</table>
</div>
Also I fixed your divs. You've got a close div in the middle of your table when your start div is before it.
<% #products.each do |product| %>
is iterating through all the elements of the #products array, which is defined in your controller.
If you want to display just one product, you first have to decide which one, then retrieve it from the database in your controller, and use it to set a variable, eg #product, which will be then available in your view.
Good luck.
<% #products.each do |product| %> is looping through all the data in your Product table.
if you open your terminal and type rake routes you'll see a list of all the routes available. Probably you'll see a listing called 'product_path'. What you see is dependent upon what's in your config/routes.rb file. You can read more about that here.
In your view, if you write <% link_to "THIS PRODUCT!", product_path(id: product.id) %> inside of the each loop, you'll be directed to the show page for that specific product (so long as you have a view for it). From there you can start building your product show page. I would suggest you check this guide out and then look up the Michael Hartl tutorial. Both are very helpful :)

rails divide loop in two tables. rails view, newbie

wondering whats the best way to split up a loop on a model in the view onto TWO tables. seems simple enough.
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
this displays the same table twice. i want to loop through the #exhibitor to fill the td's in the first table, limit 15. then continue looping through the rest of the exhibitor.name for the second table.
If you want tables of 15 do this
<% #exhibitors.each_slice(15) do |exhibitors_group| %>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% exhibitors_group.each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<% end %>
If you want first 15 and the rest in other table do this
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitor[0..15].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
<div>
<table>
<tr><th>Refreshments and Exhibits</th></tr>
<% #exhibitors[16..-1].each do |exhibitor| %>
<tr>
<td><%= exhibitor.name %></td>
</tr>
<% end %>
</table>
</div>
You should also consider 2 things:
Use a helper or a layout for those tables
Instead of slicing the array in the view do it in the controller

Resources