I want to display only a single row in the table. My controller for the same looks like this:
def Add
#item= Item.find(params[:id])
end
and my table row looks like this.. Isn't there a substitute for the method .each to just display one record?
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
In this case you don't need loop. U can directly create table like this
<tr>
<td><%= #item.name %></td>
<td><%= #item.price %></td>
<td><%= #item.quantity %></td>
<td><%= #item.quantity * #item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
#item= Item.find(params[:id])
Here, #item will be an object of class Item. So You don't need to Iterate the Object with 'each'. You can directly access the Item attribute by #item object.
#item.name
#item.price
#item.quantity.....
Related
Is there a way to do this without javascript?
Let me explain. I have two models, users and dogs.
I'm using the index as a home page for when the users are logged in.
There is a form that searches dogs (not by any information the user enters, it's prepopulated with dog data), but the index is listing the result of my search query on page load. I'd like it to only show when the button is submitted.
I'll show some code.
controller for dog:
def index
if current_user
#user = current_user
#breed = #user.dogs.first.primarybreed
# params[:search] = #breed
#dog = Dog.search(params[:search]).sample
end
end
view:
%= form_tag dogs_path, :method => :get do %>
<%= hidden_field_tag :search, #breed %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
<div id="dogswrap">
<h1><%= current_user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<td><%= image_tag #dog.image.url(:medium) %></td>
<td><%= #dog.name %></td>
<td><%= #dog.nick %></td>
<td><%= #dog.primarybreed %></td>
<td><%= #dog.secondarybreed %></td>
<td><%= #dog.age %></td>
<td><%= #dog.weight %></td>
<td><%= link_to "#{#dog.user.name}" %> </td>
<td><%= image_tag #dog.user.image.url(:thumb) %> </td>
</td>
</tr>
</tbody>
</table>
Model
def self.search(search)
find(:all, :conditions => ['primarybreed LIKE ?', "%#{search}%"])
end
I would like to have an if statement that shows the table only after the submit has been pushed.
If there is another way to do this, I'd appreciate it.
Thanks!
EDIT: Still working on this. May use a render..?
You can just use where in place of the way you're currently doing it find and conditions
def self.search(search)
where('primarybreed LIKE ?', "%#{search}%")
end
One thing I've learnt when using the LIKE function in development for SQLite3 and LIKE in PostgreSQL for production, was LIKE is case-insensitive in SQLite3 but not for PSQL. You would need to use ILIKE for that.
To your main question. This is how I would do it.
def index
if current_user
#user = current_user
#dogs = Dog.search(params[:search]) if params[:search].present?
end
end
For you views, start moving everything into a partial. _form.html.erb and _dog.html.erb
form
<%= form_tag dogs_path, :method => :get do %>
<%= text_field_tag :search %>
<%= image_submit_tag("/images/greenadd.png", size: "10x10", :name => nil) %> New Doggy Playdate!
<% end %>
dog
<td><%= image_tag dog.image.url(:medium) %></td>
<td><%= dog.name %></td>
<td><%= dog.nick %></td>
<td><%= dog.primarybreed %></td>
<td><%= dog.secondarybreed %></td>
<td><%= dog.age %></td>
<td><%= dog.weight %></td>
<td><%= link_to "#{dog.user.name}" %> </td>
<td><%= image_tag dog.user.image.url(:thumb) %></td>
Notice removed #dog and replaced with dog
Index view
<%= render 'form' %>
<div id="dogswrap">
<h1><%= #user.dogs[0].name %> Should Meet With...</h1>
<table>
<tbody>
<tr>
<% if #dogs %>
<%= render #dogs %>
<% end %>
</tr>
</tbody>
</table>
changed current_user.dogs[0].. to #user.dogs[0].. Also made a conditional for if the #dogs instance variable is truthy. If it is, then render will render out the _dog.html.erb partial for each dog in the #dogs collection
I have a table which has price and quantity fields. I want to add the price * quantity to the grand total for each item that i eventually add to the table.
My code looks like this.
<table>
<thead>
<tr>
<th width="200">Name</th>
<th width="150">Price</th>
<th>Quantity</th>
<th width="150">Total</th>
</tr>
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td class="actions">
<% link_to("update", :class => 'action update') %>
<% link_to("X", :class => 'action delete') %>
</td>
</tr>
<% end %>
</thead>
</table>
and my grand total is in the form of a label.
how do i do this? Is there static variable concept in RoR??
You should add the field grand_total in the table and create a callback in the Item model. This callback will save the value of grand total in the table each time a new item is created.
before_save : save_grand_total
def save_grand_total
self.grand_total = self.quantity * self.price
end
You could use Enumerable#inject to get the grand total as follows:
<% #= #item.inject{ |grand_total, quantity| grand_total + (quantity * cart.price) } %>
Update:
Please ignore the above line of code, that was to show you an example. The following code example should solve your issue.
Place the following line where you'd like to display the grand total.
<%= label_tag 'grand_total',
#item.inject(0) { |grand_total, item| grand_total + (item.quantity * item.price) } %>
#item.inject(0){ |grand_total, item| grand_total + (item.quantity * item.price) } applies the block to each item. The first parameter grand_total in this case is first assigned initial value of 0. This is done through inject(0).
The block then starts accumulating (item.quantity * item.price) into grand_total which is the final value that is returned by the inject.
Hope this makes sense.
<table>
<thead>
<tr>
<th width="200">Name</th>
<th width="150">Price</th>
<th>Quantity</th>
<th width="150">Total</th>
</tr>
<% grand_total=0 %>
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<% grand_total+= item.quantity * item.price %>
<td class="actions">
<% link_to("update", :class => 'action update') %>
<% link_to("X", :class => 'action delete') %>
</td>
</tr>
<% end %>
</thead>
</table>
I have the following HTML in my index file.
<% #posts.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
I have a function called is_eligible which takes post.team_name as an argument. I want to run it each "do iteration" and skip all three printing steps if it returns false
I'm not sure how to
include the file is_eligible is in and
structure the code within the html somewhere.
Any ideas?
# The view
<% #posts.each_with_index do |post, index| %>
<% unless post.is_eligible? %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
<% end %>
# The model post.rb
def is_eligible?
# and there you use self.name to access the name
end
<% #posts.select{|p| is_eligible(p. team_name)}.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
I have the following code in an ERB file:
<table border="1">
<% #lists.each do |list| %>
<tr class="even">
<td><%= link_to list.title, list %></td>
<td><%= link_to 'Edit', edit_list_path(list) %></td>
<td><%= button_to "Destroy", list_path(list), :method => :delete %></td>
</tr>
<% end %>
</table>
I want to make the <tr class="even" line dynamic. Each tr should get the class either "even" or "odd" depending on a counter variable that gets incremented every time my loop starts over. However, I cannot figure out the best way to implement this.
Use this nice helper :)
<tr class="<%= cycle("even", "odd") %>">
I'd like to view data from different database-tables in a view with tables like this picture shows:
I'm familiar with HTML tags <table>, <td> and <tr>, but I'm having trouble with multiple queries in a column.
<table>
<tr>
<th>Skills </th>
<th>Expected-qualifications</th>
<th>Current-qualifications</th>
</tr>
<% #employee.position.skills.each do |skill| %><% #employee.position.expected_qualifications.each do |expected_qualification| %><% #employee.current_qualifications.each do |current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %><% end %><% end %>
</table>
This code looks like this:
As you can see, the skills, expected-qualifications, and current-qualifications repeat.
My question: How should the codes be ordered in the table so it will look the way I want it to?
Try zip:
<% #employee.position.skills.zip(#employee.position.expected_qualifications,#employee.current_qualifications).each |skill expected_qualification current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %>
if there is REALLY can be more than one skill, expected_qualification and current_qualification so you use has_many assosiation forposition
<tr>
<td><%= #employee.position.skills.map(&:kategorien).join(", ") %></td>
<td><%= #employee.position.expected_qualifications.map(&:sollqualifikation).join(", ") %></td>
<td><%= #employee.current_qualifications.map(&:istqualifikation).join(", ") %></td>
</tr>
Otherwise you should use has_one association