It's a simple question.
For example, I have 3 data
number name country
1 Jack US
2 Coda UK
3 Fredy TW
How do I display this number in rails dynamically.
here is part of code
<% #stay_times.each do |s| %>
<tr>
<td>
<%#= I don't know what to put here %>
</td>
<td>
<%= s.name %>
</td>
<td>
<%= s.nationality %>
</td>
<% end %>
Also you can use each_with_index method:
<% #stay_times.each_with_index do |s, index| %>
<tr>
<td>
<%= index + 1 %> <!-- index starts with zero -->
</td>
<td>
<%= s.name %>
</td>
<td>
<%= s.nationality %>
</td>
<% end %>
each_with_index(*args) public
Calls block with two arguments, the item and its index, for each item
in enum. Given arguments are passed through to #each().
If no block is given, an enumerator is returned instead.
hash = Hash.new
%w(cat dog wombat).each_with_index {|item, index|
hash[item] = index
}
hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
It looks like you just need to use <%= s.number %> for the line where you have a comment. It depends on what the number field in the data table is called in your #stay_times variable. Hope this helps!
Related
I am having this scenario (main view template):
<% if #cars.count(:all) > 0 %>
<% presenter.collection.each_pair do |date, collection| %>
<tr>
<td>
...
</td>
</tr>
<%= render partial: 'car_row_content', collection: collection, as: :car, cached: Proc.new{|car| [cache_prefix, acl_fingerprint, car.record] } %>
<% end %>
<% end %>
And here's how to the partial car_row_content looks like:
<tr>
<td>
<%= car.something1 %>
</td>
<td>
<%= car.something2 %>
</td>
<td>
<%= car.something3 %>
</td>
</tr>
In this partial, the attributes something1 and something2 stay the same all the time, but the attribute something3 changes a lot (couple times a day).
What's happening here - when one of our admin changes the attribute something3, it's usually not "refreshed" immediately the main view template and we are seeing there the "old" value. How do I force Rails cache to immediately refresh when a change has happened?
Use a cache key, when you update anything, cache key will be updated and your data will be updated.
The following is intended to distribute array items into two columns.
<% #entitiquestions.in_groups_of(2, false) do |entitiquestions| %>
<tr>
<% entitiquestions.each do |entitiquestion| %>
<td>
<span data-tooltip class="hint--bottom hint--info" data-hint="<%= entitiquestion.question.query %>">
However, there is a class that should be set based on the index of the array item (if it is in fact an index...)
Can a condition be set somehow based on this position?
You can get the index of the array item by using each_with_index, for example the following will give you a row_index variable which starts from 0 for the first row:
<% #entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
Likewise, you can get the column index with:
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
Now you have the exact position of the element within the table, so you can use a ternary operator to add a class. As an example, if you wanted to add the highlight class when the row is even you could do:
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if row_index.even? %>"
Here's a full example:
<table>
<% #entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
<tr>
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
<td>
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if column_index.even? %>" data-hint="<%= entitiquestion.question.query %>">
<%= entitiquestion.question.query %>
</span>
</td>
<% end %>
</tr>
<% end %>
</table>
I have posts split into alphabetical groups like this.
A
B
C
E
Each letter needs 1 table.
Each row has 2 columns title and tags.
How do i add the table to the loop correctly so each letter has 1 table?
<table style=" width:100%; table-layout:fixed " >
</table>
index.html.erb
<% cur_letter = nil %>
<% #posts.each do |t| %>
<% title_letter = t.title[0].capitalize.match(/[A-Z]/) ? t.title[0].capitalize : "#" %>
<% if title_letter != cur_letter %>
<% cur_letter = title_letter %>
<%= cur_letter %>
<% end %>
<tr>
<td style="width:50%"><%= t.title %></td>
<td style="width:50%"><%= t.tags %></td>
</tr>
<% end %>
edit: I changed #post to #posts where stylistically appropriate
Rather than doing lots of gymnastics in your view, you could assemble the data how you want it before you pass it to the view:
#posts = Post.all.group_by {|post| post.title.downcase[0]}
# => {"a" => [<post....#array of post beginning with "A"], "b" => [post... #and so on... ]}
This will create a hash with a key representing each first letter with the value being all the Post objects whose title starts with that letter. Now with that passed to the view you could then iterate over them in a couple of ways.
In the interest of someone finding this later and running into the very real possibility that the field they are grouping by has a null value somewhere, I'll add this to cover all cases including nil values:
#posts = Post.all.group_by do |post|
if post.title == nil
post.title = ' '
end
post.title.downcase[0]
end
Now on to the view...
If you want just the letters that exist, i.e. if you don't have any posts that begin their title with "X", then you can just iterate through the keys
<% #posts.keys.sort.each do |key| %> #this is where the sorting a-z happens
<h2><%= key.upcase %></h2><br>
<table style=" width:100%; table-layout:fixed " >
<% #posts[key].each do |post| %>
<tr>
<td> <%= post.title %> </td>
<td> <%= post.tags %> </td>
</tr>
<% end %>
</table>
<% end %>
I've left out the styling details of your table as that is up to you.
If you want every letter listed, even if there are no Post titles beginning with that letter you can just do:
<% (('0'..'9').to_a + ('a'..'z').to_a).each do |letter| %>
#this generates ['0','1','2'....'z']
<h2><%= letter.upcase %></h2><br>
<table style=" width:100%; table-layout:fixed " >
<% #posts[letter].each do |post| %>
<tr>
<td> <%= post.title %> </td>
<td> <%= post.tags %> </td>
</tr>
<% end %>
</table>
<% end %>
This would give you:
A
A post beginning with "a" | some tags
Another post starting with "a" | some tags
B
C
D
Decidedly starts with "d" | some tags
Dis post starts with "d" ;-) | some tags
E
etc...
The problem with this style is that if you have any posts that start with a non-letter/non-number character, you won't see them unless you include all of those characters in your outer loop array. That is why I would do it the first way, as that represents the data that exists rather than guessing what data might exist.
I'm assuming your posts are already ordered alphabetically. You'll want to:
Open the first table.
Every time you hit a new letter, close the existing table and open a new one.
At the end, close the last table.
Code will look something like this:
<% cur_letter = nil %>
<table style=" width:100%; table-layout:fixed " >
<% #posts.each do |t| %>
<% title_letter = t.title[0].capitalize.match(/[A-Z]/) ? t.title[0].capitalize : "#" %>
<% if title_letter != cur_letter %>
<% cur_letter = title_letter %>
<%= cur_letter %>
<!-- You're switching letters here, so close the current table and start a new one. -->
</table>
<table style=" width:100%; table-layout:fixed " >
<% end %>
<tr>
<td style="width:50%"><%= t.title %></td>
<td style="width:50%"><%= t.tags %></td>
</tr>
<% end %>
</table>
What I need to do is create a page where the user will type in a last name and the system will return information related to it. I keep receiving the error "undefined method `each' for nil:NilClass." I am stuck and can not debug it any help or guidance would be greatly appreciated.
CODE FOR THE INPUT PAGE
<%= form_tag(showcustcourses_custcoursesout_path, :controller => "showcustcourses", :action => "custcoursesout", :method => "post") do %>
<div class="field">
<%= label_tag :Customer_Name %><br />
<%= text_field_tag :customer_name_in %>
</div>
<div class="actions">
<%= submit_tag "Submit Customer Name" %>
</div>
<% end %>
CODE FOR THE CONTROLLER
class BookinController < ApplicationController
def bookin
end
def bookout
#customer_name = params[:customer_name_in]
#r = Customer.find_by_last(#customer_name)
#rate_list = #r ? #r.rates : nil
end
end
CODE FOR THE OUTPUT PAGE (<% #customer_list.each do |m| %> is throwing the error)
<h1>Bookin#bookout</h1>
<p>Find me in app/views/bookin/bookout.html.erb</p>
<center><table width = 65% border = 1>
<tr> <th> Customer Name</th><th> Room Number </th> <th> Cost </th></tr>
<% #customer_list.each do |m| %>
<tr> <td> <%= m.name %> </td> <td> <%= m.roomnumber %> </td> <td> <%= m.cost %> </td> </tr>
<% end %>
</table> </center><br /> <br />
You are getting the error undefined method 'each' for nil:NilClass because you forgot to set the value of instance variable #customer_list. So, #customer_list is nil.
You need to set #customer_list variable in the action corresponding to your view which in your case is bookout action as you are rendering bookout.html.erb.
Simply, do this in BookinController#bookout:
def bookout
## ...
#customer_list = Customer.all ## Add this
end
UPDATE
As per the chat discussion, OP needed to show last(from customers table), roomlevel(from apples table), cost(from rates table)
Suggested to modify
bookout method as below:
def bookout
#customer_list = Customer.all
#customer_name = params[:customer_name_in]
end
and bookout.html.erb as below:
<% #customer_list.each do |customer| %>
<% customer.bookings.each do |booking| %>
<tr>
<td> <%= customer.last %> </td>
<td> <%= booking.apple.room_level %> </td>
<td> <%= booking.apple.cost %> </td>
</tr>
<% end %>
<% end %>
Also, OP's schema was not correct to achieve this result. Added apple_id to bookings table and removed rate_id from it.
NOTE: As you don't want bookings to be associated with rates table,rate_idwas removed from bookings table. You would have to add cost field in apples table to display the cost in the view.
Add this in your controller. It will bring details of all customers.
def bookin
#customer_list = Customer.all
end
def bookout
#customer_list = Customer.all
#customer_name = params[:customer_name_in]
#r = Customer.find_by_last(#customer_name)
#rate_list = #r ? #r.rates : nil
end
If still it returns nil error, then it means you do not have any customer records in database.
<h1>Bookin#bookout</h1>
<p>Find me in app/views/bookin/bookout.html.erb</p>
<center><table width = 65% border = 1>
<tr> <th> Customer Name</th><th> Room Number </th> <th> Cost </th></tr>
#check if object is not nil
<% if !#customer_list.nil? %>
<% #customer_list.each do |m| %>
<tr>
<td> <%= m.name %> </td>
<td> <%= m.roomnumber %> </td>
<td> <%= m.cost %>
</td>
</tr>
<% end %>
<% else %>
<p> no customers available </p>
<% end %>
</table>
</center>
#customer_list is not defined
defined it in controller like this
#customer_list = Customer.all
Beneficiaries
id (PK)
name
age
income
Beneficiary_loans
id (PK)
beneficiary_id (FK)
amount
rate
period
What I'm doing is, selecting a list of beneficiary from [Beneficiaries] table and displaying as follows
<%= form_tag({:action => 'update_survey_list_status', :status=>4}) do %>
<table width="100%">
<tr>
<th>Beneficiary Details</th>
<th>Amount</th>
<th>Rate</th>
<th>Period</th><th>
<input type='checkbox' name='checkall' onclick='checkedAll();'>
</th>
</tr>
<% #publishedlist.each do |b| %>
<tr>
<td><%= b.firstname %></td>
<%= fields_for :beneficiaryloan do |bloan| %>
<td> <%= bloan.text_field :amount%></td>
<td> <%= bloan.text_field :rate%></td>
<td> <%= bloan.text_field :period%></td>
<% end %>
<td><%= check_box_tag "benificiary_ids[]",b.id, :name => "benificiary_ids[]"%> </td>
</tr>
<% end %>
</table> <%= submit_tag "Approve", :class=>'form_buttons' %>
<% end %>
In controller,
#beneficiaries=Beneficiary.find(:all, :conditions => ["id IN (?)", params[:benificiary_ids]])
#beneficiaries.each do |b|
#beneficiaryloan = Beneficiaryloan.new(params[:beneficiaryloan])
#beneficiaryloan.beneficiary_id=b.id
#beneficiaryloan.hfi_id=session[:id].to_s
#beneficiaryloan.status_id=params[:status]
#beneficiaryloan.save
end
What I'm not getting is
params[:beneficiaryloan]
Values are coming as NULL
Am I missing something here in this form?
Check the following,
With the help of any browser tool(eg:firebug), make sure that the form has been rendered properly. Sometimes due to some misplaced tags(generally happens with table structure) the form does not renders properly.
Try to remove the table structure and see if the form works.
Make sure your association is fine, i guess it should be Beneficiary has many Beneficiaryloan
If none of the above helps, please post the request parameters over here.