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>
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
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 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.
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.