In Rails show method is not getting executed - ruby-on-rails

Frnds I have created a controller, named products_controller and product model and also index.html.erb and show.html.erb
but when i call localhost:3000/products it get the details from database. in the same page i have given a link to show the details of a product clearly. But it is not loading. when i clicks on show link i has to get the details of each product. but localhost:3000/products), it loads to the same page.
result for localhost:3000/products
Model Name Brand Name Price Discount Qty Available
Nokia Lumia Nokia 15000 5.0 25 Show
Samsung galaxy Nexus Samsung 45000 15.0 5 Show
Sony Experia Sony Ericsson 4500 1.0 10 Show
Nikon D5100 Nikon 25500 10.0 100 Show
Panasonic Lumix Panasonic 25500 10.0 104 Show
Olympus Granite Olympus 24520 12.0 45 Show
products_controller.rb
class ProductsController < ApplicationController
def index
#products = Product.select("model_name,brand_name, price, discount, qty_avaliable")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
#render :text => params and return false
def show
#product = Product.find_by_sql("select model_name,brand_name, price, discount, qty_avaliable from products where prod_id='PR1'")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
end
product.rb
class Product < ActiveRecord::Base
attr_accessible :prod_id, :model_name, :brand_name, :price, :discount, :qty_avaliable
end
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Products List</h1>
<table>
<tr>
<th>Model Name</th>
<th>Brand Name</th>
<th>Price</th>
<th>Discount</th>
<th>Qty Available</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #products.each do |product| %>
<tr>
<td><%= product.model_name %></td>
<td><%= product.brand_name %></td>
<td><%= product.price %></td>
<td><%= product.discount %></td>
<td><%= product.qty_avaliable %></td>
<td><%= link_to 'Show', Product %></td>
</tr>
<% end %>
</table>
show.html.erb
<p prod_id="notice"><%= notice %></p>
<p>
<b>Model Name:</b>
<%= #product.model_name %>
</p>
<p>
<b>Brand Name:</b>
<%= #product.brand_name %>
</p>
<p>
<b>Price:</b>
<%= #product.price %>
</p>
<p>
<b>Discount:</b>
<%= #product.discount %>
</p>
<p>
<b>Quantity:</b>
<%= #product.qty_avaliable %>
</p>

First thing, why don't you have an id for your model, and why are you doing raw SQL?
It should be:
class ProductsController < ApplicationController
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
def show
#product = Product.where(id: params[:id]).first
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
end
And on your views, the link is wrong, you should pass the id for the current product in the iteration
<% #products.each do |product| %>
<tr>
<td><%= product.model_name %></td>
<td><%= product.brand_name %></td>
<td><%= product.price %></td>
<td><%= product.discount %></td>
<td><%= product.qty_avaliable %></td>
<td><%= link_to('Show', product) %></td>
</tr>
<% end %>
link_to will do some meta programming behind the scenes, but its mostly the same than doing
<td><%= link_to('Show', product_path(id: product.id) %></td>

It seems to me, that you should use <%= link_to 'Show', product %> instead of <%= link_to 'Show', Product %> in index.html.erb

Here:
<td><%= link_to 'Show', Product %></td>
You need to use product, which is the block variable, not Product, which is a class.

Related

Index view of Rails model printing string with all model data

I just finished Rails' Getting Started guide and everything works perfectly except for this mysterious entry that's being printed in the Index view as shown below. I've been trying to find the cause to no avail, neither have I found no suitable terms to Google this issue.
Index.html.erb
<h1>Index</h1>
<%= link_to 'New Client', new_client_path %>
<table>
<tr>
<th>Name</th>
<th>Issued On</th>
<th>Notes</th>
<th>Finished?</th>
<th>Payments</th>
</tr>
<%= #clients.each do |client| %>
<tr>
<td><%= client.name %></td>
<td><%= client.date %></td>
<td><%= client.note %></td>
<td><%= client.finished %></td>
<td><%= client.payment %></td>
<td><%= link_to 'Show', client_path(client) %></td>
<td><%= link_to 'Edit', edit_client_path(client) %></td>
<td><%= link_to 'Destroy', client_path(client),
method: :delete,
data: { confirm: 'This client will be permanentally deleted, do you want to continue?' } %></td>
</tr>
<% end %>
</table>
Clients Controller
class ClientsController < ApplicationController
def index
#clients = Client.all
end
def show
#client = Client.find(params[:id])
end
def new
#client = Client.new
end
def edit
#client = Client.find(params[:id])
end
def create
#client = Client.new(client_params)
if #client.save
redirect_to #client
else
render 'new'
end
end
def update
#client = Client.find(params[:id])
if #client.update(client_params)
redirect_to #client
else
render 'edit'
end
end
def destroy
#client = Client.find(params[id])
#client.destroy
redirect_to clients_path
end
private
def client_params
params.require(:client).permit(:name, :date, :note, :finished, :payment)
end
end
Remove = from <%= #clients.each do |client| %>. It is outputting result of each which you don't need to print.
In ERB,
<%= %>
executes ruby code as well as outputs the result
For example: <%= client.name %>
<% %>
executes ruby code but doesn't output the result
For example: <% #clients.each do |client| %>....<% end %>

How to display a column of a table associated to another table while exporting data to excel sheet?

I am trying to export data from documents table to spreadsheet using spreadsheet and to_xls gem.
I am following this tutorial:
http://code.dblock.org/2011/09/01/exporting-data-to-excel-in-ruby-on-rails-w-spreadsheet-and-toxls.html
Now a
service has_many: documents
document belongs_to: service
The service table has a column called name. So instead of displaying service_id on my excel sheet , I want to display service.name
documents/views/index.html.erb
<table id='myDocumentsTable' class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Service</th>
<th>Tat</th>
<th>Automated</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #documents.each do |document| %>
<tr>
<td><%= document.name %></td>
<td><%= document.service.name %></td>
<td><%= document.tat %></td>
<td><%= document.automated %></td>
<td><%= document.default_price %></td>
<td><%= link_to 'Show', document %></td>
<td><%= link_to 'Edit', edit_document_path(document) %></td>
<td><%= link_to 'Destroy', document, method: :delete, data: {
confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= will_paginate #documents %>
<%= link_to 'New Document', new_document_path, class: 'new_doc' %>
<%= link_to 'Export', documents_path(request.parameters.merge({:format
=> :xls})), :class =>"btn btn-primary" %>
documents_controller.rb
def index
#documents = Document.all.paginate(:page => params[:page], :per_page =>
30).order('name ASC')
respond_to do |format|
format.html
format.xls { send_data Document.all.select([:name, :service_id , :tat,
:automated, :default_price]).to_xls, content_type: 'application/vnd.ms-
excel', filename: 'documents.xls' }
end
end
I want to display the exact contents of index.html.erb into an excel sheet where the service column should contain the service.name not the service_id.
How can I achieve this? Please help!
You can join the service and use select to get specific attributes from services table
You can use service.name as service_name which will return you service name
def index
#documents =
Document.all.paginate(page: params[:page], per_page: 30).order('name ASC')
respond_to do |format|
format.html
format.xls do
send_data(
Document.joins(:service)
.select('documents.name, services.name as service_name,
documents.tat, documents.automated,
documents.default_price'
).to_xls,
content_type: 'application/vnd.ms-excel',
filename: 'documents.xls'
)
end
end
end

Rails - best_in_place not updating view until after reload

I'm pretty new at Ruby on Rails and I'm struggling with best_in_place
I'm using best_in_place to enable editing of data within a HTML table. I'm able to successfully enter data and hit enter for it to save. The new data briefly shows in the table, but it soon reverts back to the previous value. If I reload the page the table will show the new, updated data. Does anyone have any suggestion how to make the updated data show straight away?
This is the code for my controller:
class GamesController < ApplicationController
def index
#games = Game.all
end
def show
#game = Game.find(params[:id])
#render 'index'
end
def new
#game = Game.new
end
def edit
#game = Game.find(params[:id])
end
def create
#game = Game.new(game_params)
if #game.save
redirect_to #game
else
render 'new'
end
end
def update
#game = Game.find(params[:id])
if #game.update(game_params)
redirect_to #game
else
render 'edit'
end
end
def destroy
#game = Game.find(params[:id])
#game.destroy
redirect_to games_path
end
private
def game_params
params.require(:game).permit(:number, :title, :weight, :owner, :borrower, :borrowed)
end
end
And this is the code for the view:
<h1>Listing games</h1>
<%= link_to 'New game', new_game_path, :class => "pure-button pure-button-primary" %>
<p>
<table class="pure-table sortable">
<thead>
<tr>
<th width="110px">Number</th>
<th width="300px" >Title</th>
<th width="110px">Weight</th>
<th width="110px">Owner</th>
<th width="120px">Borrowed</th>
<th width="140px">Borrowed by</th>
<th class="sorttable_nosort">Action</th>
</tr>
</thead>
<tbody>
<% #games.each do |game| %>
<tr>
<td><%= best_in_place game, :number, :inner_class => "input_number" %></td>
<td><%= best_in_place game, :title , :inner_class => "input_title" %></td>
<td><%= best_in_place game, :weight, :inner_class => "input_weight" %></td>
<td><%= best_in_place game, :owner, :inner_class => "input_owner" %></td>
<td><%= best_in_place game, :borrowed, as: :checkbox, collection: {false: "No", true: "Yes"} %></td>
<td><%= best_in_place game, :borrower, :inner_class => "input_borrower" %></td>
<td><%= link_to 'Show', game_path(game) %> |
<%= link_to 'Edit', edit_game_path(game) %> |
<%= link_to 'Destroy', game_path(game),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</p>
</div>
<%= render 'grid_bottom' =%>
Any help or suggestion is greatly appreciated!

Ajax edit page in index

I have this page Index page:
<h1>Listing users</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>Who</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.description %></td>
<td><%= user.who %></td>
<%= render 'users/form' %>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
Scaffold controller
def index
#users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
end
I need to edit user in index page, not to go url /users/2/edit, stay on users page.
How I do this in Ajax?
Add remote:true to the Edit line in order to make an ajax call:
<td><%= link_to 'Edit', edit_user_path(user), remote:true %></td>
Also, add an edit.js.erb file (a javascript file with embedded ruby) in which you do all the javascript actions to be executed after the Edit. That edit.js.erb file is generated when the edit method is done. This is where you write all the changes to the javascript code which affect the current page (the page will not be re-rendered).
Additionally, add the following to the edit method:
respond_to do |format|
format.js
end

ruby printing database content

I am new in Ruby.
I have to render the database structure, using tutorial i found suitable code:
File index.html.erb:
<h1>Listing tasks</h1>
<table>
<tr>
<th>Title</th>
<th>Content</th>
<th>Date From</th>
<th>Date To</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.content %></td>
<td><%= task.datefrom %></td>
<td><%= task.dateto %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Now i want to group tasks by 'date from' value, probably it means that in index.html.erb i should print only dates, and clicking on the date i should call another html file that will render tasks by chosen date.
this can be like
File index.html.erb:
<h1>Listing dates</h1>
<table>
<tr>
<th>Date From</th>
<% #tasks.each do |task| %>
<tr>
<td><%= !!! IF DATE NOT PRINTED THEN PTINT IT task.datefrom %></td>
<td><%= link_to 'Edit', edit_date_path(task, date) %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Where edit_date_path(task, date) should refer me to the index_date.html.erb, where i can get selected date and print tasks according to selected date.
Maybe i can get suggestion, it will be much easier is fomebody can help me with that, as task should not be very difficult, but otherwise i can waste quite a lot of time googling.
Thanks,
Urmas.
Editing the question.
This helped a little. what i did now, i changed index.html.erb to
<h1>Listing dates</h1>
<table>
<tr>
<th>Date To</th>
<th></th>
</tr>
<% dates = Array.new %>
<% #tasks.each do |task| %>
<% if ( !dates.include?(task.dateto.strftime("%m.%d.%Y")) ) then
dates.push task.dateto.strftime("%m.%d.%Y")
%>
<tr>
<td><%= task.datefrom.strftime("%m.%d.%Y") %></td>
<td><%= link_to 'Show Date', {:action => "index", :date => task.dateto} %></td> <-- This does not work
</tr>
<%
end
%>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Where 'Show Date' link link should be made to show_date.html.erb, where is correct code for showing records when input date is passed.
I had added in controller also method
def show_date
#tasks = Task.find(params[:dateto])
#dateto = :dateto
respond_to do |format|
format.html # showdate.html.erb
format.json { render :json => #tasks }
end
end
that can be used in the not working link, to pass data to the 'Show Date' (show_date.html.erb), but i get errors all the time. The problem is with correct calling off the show_date.html.erb, code i will be able to write myself :)
Urmas
in Tasks controller:
add a method
def index_date
#tasks = Task.all
end
and create a index_date.html.erb write
<% #tasks.each do |task| %>
<tr> <td><%= task.datefrom %></td>
<td><%= link_to 'Edit', {:action => "index", :date => task.datefrom} %></td>
</tr>
<%end%>
and in the index method in the controller change
#tasks = Task.all to
#tasks = Task.find_all_by_datefrom(#{params[:date])
This will list the tasks that match the date selected and everything will be similar as it is now thereafter.
Found the solution.
It were necessary to add to routes.rb
resources :tasks do
member do
get 'showdate'
end
get "home/index"
end
add corresponding controller processing
def showdate
#task = Task.find(params[:id])
#tasks = Task.find(:all)
respond_to do |format|
format.html # showdate.html.erb
format.json { render :json => #tasks }
end
end
and to call it using (index.html.erb)
<td><%= link_to 'Show Date', showdate_path(task.id) %></td>
All further processing is already in showdate.html.erb.
Hope this helps somebody.
I AM THE BEST!!!! xD

Resources