I am getting data in from a controller and its printed through a loop. This is the example from the getting started guide. I want each row to have class in numbered in order like, class-1 class-2 etc.
Below is my code in view file.
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post_path(post) %> | </td>
<td><%= link_to 'Edit', edit_post_path(post) %> | </td>
<td><%= link_to 'Delete', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
For example each row should be having classes in order like below when rendered:
<tr class="class-1">
<td>title</td>
</tr>
<tr class="class-2">
<td>title</td>
</tr>
<tr class="class-3">
<td>title</td>
</tr>
Something like
<% classid = 0 %>
<% #posts.each do |post| %>
<%= "<tr class=\"class-#{classid}\">" %>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', post_path(post) %> | </td>
<td><%= link_to 'Edit', edit_post_path(post) %> | </td>
<td><%= link_to 'Delete', post_path(post),
method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% classid +=1 %>
<% end %>
That said I'd be looking at presenter pattern, or a method on whatever class post is or better still if post is in the db, using it's id instead of sequential number.
Related
I am trying to replace the table tag in my html code with p tags denoting that "Result not found!" after searching but I am unsure how.
Controller
def index
if params[:search]
#parameter = params[:search]
#students = Student.all.where("name LIKE :search",search: #parameter)
if #students.blank?
redirect_to students_path
end
else
#students = Student.all
end
end
HTML
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h1>Listing students</h1>
</div>
<div id="body" class="col-md-12">
<%= form_tag students_path, :method => 'get' do%>
<p>
<%= text_field_tag :search,params[:search]%>
<%= submit_tag "Search"%>
</p>
<%end%>
<%= link_to 'New student', new_student_path %>
<table class="table">
<tr>
<th>Name</th>
<th>ID</th>
<th>Course</th>
<th></th>
</tr>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<td><%= student.student_id %></td>
<td><%= student.course %></td>
<td><%= link_to 'Show', student_path(student) %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student_path(student), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
For now, I redirect to the index page as a stand-in. Any advice?
You can solve the problem in html
<% if #students.present? %>
<table class="table">
<tr>
<th>Name</th>
<th>ID</th>
<th>Course</th>
<th></th>
</tr>
<% #students.each do |student| %>
<tr>
<td><%= student.name %></td>
<td><%= student.student_id %></td>
<td><%= student.course %></td>
<td><%= link_to 'Show', student_path(student) %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student_path(student), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<% else %>
<p>Result not found!</p>
<% end %>
I have a Model called Categories, which I wanna display. Therefore I have inputed the following statement in the pages/home.html.erb
<%= render 'categories/index.html.erb' %>
Now whenever I run the server I get a NoMethodError for the line :
<% #categories.each do |category| %>
This is the full index.html.erb file of the categories view:
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
<p id="notice"><%= notice %></p>
<h1>Categories</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #categories.each do |category| %>
<tr>
<td><%= category.title %></td>
<td><%= category.price %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Category', new_category_path %>
Can someone help me understand why this is not working?
When you want to render the categories/index.html.erb partial on your homepage too, then you must initialize all variables that are used in that partial.
Because you use #categories in that partial you will need to add the following to your controller action. I assume that you have a PagesController with a home method already.
# in app/controllers/pages_controller.rb
def home
#categories = Category.all # <= Add this line
end
1.This code of welcome/index.html.erb:
<%= form_tag('search', method:"get",remote:true) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
<div id="results">
<%= render 'searchresults' %>
</div>
2.This code of _searchresults.html.erb:
<table class="table">
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
3. This code of WelcomeController:
class WelcomeController < ApplicationController
def index
#articles = Article.all
return #articles
end
def search
#query = params[:q]
#articles = Article.where('name LIKE ?', "%#{#query}%")
respond_to do |format|
format.html { redirect_to #articles }
format.js
end
end
end
Add routes:
root 'welcome#index'
get 'welcome/search'
code of search.js.erb:
$('#results').html("<%= render 'searchresults' %>")
Result
Why ajax of Rails Application not working?
Thanks all.
I using rails 5.2, ruby 2.5.
<div id="results">
<%= render 'searchresults', articles: #articles %>
</div>
It's always a good practice to pass local variable in partial-
$('#results').html("<%= j render 'welcome/searchresults', articles: #articles%>");
In _searchresults.html.erb
<table class="table">
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
</tr>
<% articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
I have two models here for Item and RecipeIngredient as below where RecipeIngredient belongs to Item.
Model for Item
class Item < ApplicationRecord
belongs_to :item_type, :class_name=>ItemType, :foreign_key=>"item_type_id"
end
Model for RecipeIngredient
class RecipeIngredient < ApplicationRecord
belongs_to :item, :class_name=>Item, :foreign_key=>"item_id"
belongs_to :ingredient, :class_name=>Ingredient, :foreign_key=>"ingredient_id"
validates_numericality_of :quantity
end
I have index page for Items where I have created a link to index page for RecipeIngredients and I am passing the id of the item in the URL as parameter.
Index Page for Item
<p id="notice"><%= notice %></p>
<h1>Items</h1>
<table>
<thead>
<tr>
<th>Item</th>
<th>Item type</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #items.each do |item| %>
<tr>
<td><%= item.item %></td>
<td><%= item.item_type.item_type %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Add Recipe', recipe_ingredients_path(:item_id =>item.id) %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Item', new_item_path %>
<%= link_to "Home", '/' %>
And I have the controller for RecipeIngredients as below.
Controller for RecipeIngredient
def index
#recipe_ingredients = RecipeIngredient.find(params[:item_id])
end
And the index page for recipe ingredients looks like as given below. I need to filter the data displayed on this index page of recipe ingredients only to match the item_id received as a parameter in URL.
Index for RecipeIngredients
<p id="notice"><%= notice %></p>
<h1>Recipe Ingredients</h1>
<table>
<thead>
<tr>
<th>Item</th>
<th>Ingredient</th>
<th>Quantity</th>
<th>Unit</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #recipe_ingredients.each do |recipe_ingredient| %>
<tr>
<td><%= recipe_ingredient.item.item %></td>
<td><%= recipe_ingredient.ingredient.ingredient %></td>
<td><%= recipe_ingredient.quantity %></td>
<td><%= recipe_ingredient.ingredient.recipe_unit %></td>
<td><%= link_to 'Show', recipe_ingredient %></td>
<td><%= link_to 'Edit', edit_recipe_ingredient_path(recipe_ingredient) %></td>
<td><%= link_to 'Destroy', recipe_ingredient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Recipe Ingredient', new_recipe_ingredient_path %>
<%= link_to 'Back', '/items' %>
Right now I am getting this error:
undefined method `show' for #< RecipeIngredient:0xa5653b8>
#recipe_ingredients = RecipeIngredient.find(params[:item_id])
This will return you only one RecipeIngredient with id = params[:item_id] not activerecord array
So you need to change find to where if you want to loop over #recipe_ingredients
#recipe_ingredients = RecipeIngredient.where(params[:item_id])
or, you need to change the view
<tbody>
<tr>
<td><%= #recipe_ingredients.item.item %></td>
<td><%= #recipe_ingredients.ingredient.ingredient %></td>
<td><%= #recipe_ingredients.quantity %></td>
<td><%= #recipe_ingredients.ingredient.recipe_unit %></td>
<td><%= link_to 'Show', #recipe_ingredients %></td>
<td><%= link_to 'Edit', edit_recipe_ingredient_path(#recipe_ingredients) %></td>
<td><%= link_to 'Destroy', #recipe_ingredients, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</tbody>
I'm trying to build a real simple event listing app that list a bunch of event based on Dates. I'm fairly new and was wondering how would I display a list or table based on dates? This is what I have now.
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Link</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I would like it display in a date format. For example all events under the date of February/21 will appear under that specific heading.
Thanks!
You can try something like this:
<% #events.group_by(&:date).each do |date, events| %>
<tr><td colspan="7"><h1><%= date %></h1></td></tr>
<% events.each do |event| %>
<tr>
<td><%= event.title %></td>
<td><%= event.description %></td>
<td><%= event.link %></td>
<td><%= event.date %></td>
<td><%= link_to 'Show', event %></td>
<td><%= link_to 'Edit', edit_event_path(event) %></td>
<td><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>