Learning Ruby on Rails. Syntax issue - ruby-on-rails

I'm new to Ruby, Rails and programming in general, so please forgive me if the question is very trivial.
I have this view:
<h1>Listing products</h1>
<table>
<tr>
<th>\</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #products.each do |product| %>
<tr>
<td><%= product.\ %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
When I try to access the link http://localhost:3000/products I receive an error:
SyntaxError in Products#index
Showing C:/Sites/depot/app/views/products/index.html.erb where line #13 raised:
C:/Sites/depot/app/views/products/index.html.erb:13: syntax error, unexpected $undefined
...tput_buffer.append= ( product.\ );#output_buffer.safe_concat...
... ^
C:/Sites/depot/app/views/products/index.html.erb:18: syntax error, unexpected keyword_end, expecting ')'
'); end
^
C:/Sites/depot/app/views/products/index.html.erb:25: syntax error, unexpected keyword_ensure, expecting ')'
C:/Sites/depot/app/views/products/index.html.erb:27: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #13):
10:
11: <% #products.each do |product| %>
12: <tr>
13: <td><%= product.\ %></td>
14: <td><%= link_to 'Show', product %></td>
15: <td><%= link_to 'Edit', edit_product_path(product) %></td>
16: <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
Trace of template inclusion: app/views/products/index.html.erb
I was just following an example in a book to learn Rails, so basically I've done nothing to code this view.
Can someone point me to the right direction?

You need to put <%= product.name %> (substitute in desired attribute) instead of <%= product.\ %>
The little tiny caret under the the incorrect syntax gives you an indication of what is tripping the error.

Rewrite the code # app\views\products\index.html.erb" as follows:
<h1>Listing products</h1>
<table>
<% #products.each do |product| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<td>
<%= image_tag(product.image_url, class: 'list_image') %>
</td>
<td class="list_description">
<dl>
<dt><%= product.title %></dt>
<dd><%= truncate(strip_tags(product.description),
length: 80) %></dd>
</dl>
</td>
<td class="list_actions">
<%= link_to 'Show', product %><br/>
<%= link_to 'Edit', edit_product_path(product) %><br/>
<%= link_to 'Destroy', product, method: :delete,
data: { confirm: 'Are you sure?' } %>
</td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New product', new_product_path %>
Rewrite the code # app\controllers\products_controller.rb as follows:
class ProductsController < ApplicationController
# GET /products
# GET /products.json
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
# GET /products/1
# GET /products/1.json
def show
#product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
# GET /products/new
# GET /products/new.json
def new
#product = Product.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #product }
end
end
# GET /products/1/edit
def edit
#product = Product.find(params[:id])
end
# POST /products
# POST /products.json
def create
#product = Product.new(params[:product])
respond_to do |format|
if #product.save
format.html { redirect_to #product,
notice: 'Product was successfully created.' }
format.json { render json: #product, status: :created,
location: #product }
else
format.html { render action: "new" }
format.json { render json: #product.errors,
status: :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.json
def update
#product = Product.find(params[:id])
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to #product,
notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #product.errors,
status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product = Product.find(params[:id])
#product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
end

rails generates very useful logs. Read it once it gives you line number and file name.
replace <%= product.\%> with <%= product.name %> where name is the attribute of product.

Related

Running into undefined method from model class in view

I have a method called calculation_of_total_cost in model Tippy
It's running into problems being called in index.html.erb via tippies views directory.
This is the error I receive: undefined method*' for nil:NilClass`
I have googled it, and now understand that it is the result of the one of the variables being nil.
How do I resolve this, i.e, how do I make the method work in index.html.erb? This is index view that I am calling it from, so I need an instance method, not class, right?
Also, addendum: this same method works fine in show.html.erb
show.html.erb
<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />
<table class="table table-striped">
<tr>
<td>
Cost of Your Meal:
</td>
<td>
<%= humanized_money_with_symbol #tippy.cost %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(#tippy.tip * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol #tippy.calculation_of_total_cost %>
</td>
</tr>
</table>
<%= link_to 'New Tippy', new_tippy_path %>
<%= link_to "Index", tippies_path %>
Here is the Tippy model:
class Tippy < ApplicationRecord
validates :tip, presence: true
validates :cost, presence: true
#monetize :tip_cents
monetize :cost_cents, :numericality => {:greater_than => 0}
TIP_CHOICES = { "10%" => ".10", "20%" => ".20", "30%" => ".30", "40%" => ".40", "50%" => ".50",
"60%" => ".60", "70%" => ".70", "80%" => ".80", "90%" => ".90" }
def calculation_of_total_cost
cost + (tip * cost)
end
end
Here is the index.html.erb file
<p id="notice"><%= notice %></p>
<h1>Tippies</h1>
<table>
<thead>
<tr>
<th>Tip</th>
<th>Cost</th>
<th>Total</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tippies.each do |tippy| %>
<tr>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tippy', new_tippy_path %>
Tippy Controller
class TippiesController < ApplicationController
#before_action :set_tippy, only: [:show, :edit, :update, :destroy]
# GET /tippies
# GET /tippies.json
def index
#tippies = Tippy.all
end
# GET /tippies/1
# GET /tippies/1.json
def show
##calculation_of_total_cost
end
# GET /tippies/new
def new
#tippy = Tippy.new
end
# GET /tippies/1/edit
def edit
end
# POST /tippies
# POST /tippies.json
def create
#tippy = Tippy.new(tippy_params)
respond_to do |format|
if #tippy.save
format.html { redirect_to #tippy, notice: 'Tippy was successfully created.' }
format.json { render :show, status: :created, location: #tippy }
else
format.html { render :new }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tippies/1
# PATCH/PUT /tippies/1.json
def update
respond_to do |format|
if #tippy.update(tippy_params)
format.html { redirect_to #tippy, notice: 'Tippy was successfully updated.' }
format.json { render :show, status: :ok, location: #tippy }
else
format.html { render :edit }
format.json { render json: #tippy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tippies/1
# DELETE /tippies/1.json
def destroy
#tippy.destroy
respond_to do |format|
format.html { redirect_to tippies_url, notice: 'Tippy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tippy
#tippy = Tippy.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tippy_params
params.require(:tippy).permit(:tip, :cost)
end
end
To solve this problem you need to set a binding.pry or a breakpoint at this line of index.html.erb, so that we can understand in the loop you are executing why tippy is getting value of nil.
You need to install pry gem.
Please also share the values of #tippies and the details of the other variable in the loop that fails, because tippy=nil.
An alternative for pry is just printing the value of the variable in the log with puts tippy.calculation_of_total_cost.
Right now I am guess is that #tippies which includes all #tippy in your tippies table, could have one field that has calculation of total cost = nil. To verifiy this you should check with the debug the value of tippy and of tippy.calculation_of_total_cost in the index.html.erb view.
<% #tippies.each do |tippy| %>
<tr>
<% binding.pry %>
<td><%= tippy.tip %></td>
<td><%= tippy.cost %></td>
<td><%= tippy.calculation_of_total_cost %></td>
<td><%= link_to 'Show', tippy %></td>
<td><%= link_to 'Edit', edit_tippy_path(tippy) %></td>
<td><%= link_to 'Destroy', tippy, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
also it is a good idea to inspect show.html.erb as there it is working.
def calculation_of_total_cost
cost + (tip * cost)
end

ActiveRecord::RecordNotFound in TodosController#index

The full text of the error message is:
ActiveRecord::RecordNotFound in TodosController#index
Couldn't find User with id=2
Rails.root: /home/randy/rubystack-1.9.3-29/projects/chap14
app/controllers/application_controller.rb:11:in current_user
app/views/todos/index.html.erb:21:in _app_views_todos_index_html_erb___949818655437808348_39324440
app/controllers/todos_controller.rb:8:in index
Here's the code for my application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
if session[:user_id]
#current_user ||= User.find(session[:user_id])
else
#current_user = nil
end
end
def check_login
unless authorized?
redirect_to "/auth/identity"
end
end
def logged_in?
if session[:user_id]
return true
else
return false
end
end
protected
def authorized?
logged_in? && (request.get? || current_user.admin?)
end
end."
And here's my todos_controller.rb:
class TodosController < ApplicationController
before_filter :check_login
# GET /todos
# GET /todos.json
def index
#todos = Todo.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #todos }
end
end
# GET /todos/1
# GET /todos/1.json
def show
#todo = Todo.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #todo }
end
end
# GET /todos/new
# GET /todos/new.json
def new
#todo = Todo.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #todo }
end
end
# GET /todos/1/edit
def edit
#todo = Todo.find(params[:id])
end
# POST /todos
# POST /todos.json
def create
#todo = Todo.new(params[:todo])
respond_to do |format|
if #todo.save
format.html { redirect_to #todo, notice: 'Todo was successfully created.' }
format.json { render json: #todo, status: :created, location: #todo }
else
format.html { render action: "new" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# PUT /todos/1
# PUT /todos/1.json
def update
#todo = Todo.find(params[:id])
respond_to do |format|
if #todo.update_attributes(params[:todo])
format.html { redirect_to #todo, notice: 'Todo was successfully updated.' }
format.json { head :no_content }
#todo = Todo.find(params[:id])
if #todo.completed == true
#todo.user_who_completed = current_user.email
#todo.save
end
else
format.html { render action: "edit" }
format.json { render json: #todo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /todos/1
# DELETE /todos/1.json
def destroy
#todo = Todo.find(params[:id])
#todo.destroy
respond_to do |format|
format.html { redirect_to todos_url }
format.json { head :no_content }
end
end
end."
And my app/views/todos/index.html.erb file:
"<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% end %>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>S
</tr>
<% end %>
</table>
<br />
<% if current_user.admin? %>
<%= link_to 'New Todo', new_todo_path %>
<% end %>
I'm new to rails and have no clue about what the error message means, nor its cause. I want the visitor to land on the todos/index.html page. I have my root route set accordingly. Would appreciate some help.
That error is coming from User.find. It's described in the Documentation for find.
It's telling you that there is no User that has that id. Somehow you have saved a user id into your session that corresponds to a user that doesn't currently exist.
You should probably examine how you are storing user ids to the session and under what circumstances that user could be getting deleted.
The problem is with your loop. If conditional it is outside the cycle.
Check solution view:
<h1>Listing todos</h1>
<table>
<tr>
<th>Name</th>
<th>Completed</th>
<th>Completed date</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for todo in #todos %>
<tr>
<td><%= todo.name %></td>
<td><%= todo.completed %></td>
<td><%= todo.completed_date %></td>
<td><%= todo.user %></td>
<% if current_user.admin? %>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<% end %>
</table>
Recommendations
Check your question. You have the code view with the controller code.
Be careful with indentation.

Why is only part of my code not rendering in view

While I was working trying to code my views, I noticed that my code that was previously rendering on the index view is now only showing the first two lines of code on my local server, and I don't understand why.
Here is my index.html.erb code:
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<div class="row">
<div class="col-md-8">
<tbody>
<% #bookmarks.each do |bookmark| %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
And here is my bookmark controller code:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Any thoughts?
It seems that your html is invalid. You are using div tags in rails loop, which is not being closed. The other thing is that non-table related html tags can be used only inside tags.
This might be the working solution.
<h1>All Bookmarks</h1>
<%= link_to 'Create a New Bookmark', new_bookmark_path %>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #bookmarks.each do |bookmark| %>
<tr>
<td><%= link_to bookmark.url, "http://#{bookmark.url}" %></td>
<td><%= link_to 'Show', bookmark %></td>
<td><%= link_to 'Edit', edit_bookmark_path(bookmark) %></td>
<td><%= link_to 'Destroy', bookmark, method: :delete, data: { confirm: 'Are you sure?' } %> </td>
</tr>
<% end %>
</tbody>
</table>
Hope that helps.

Scaffold Rails Application Removed Show Action, Now Edit/Destroy No Longer Works

Hi I generated a scaffold for my rails application. I put in resources for that model in the config file. I figure I can cut/modify what scaffold generated later. I removed the show method/view etc and now destroy/edit no longer works.
Turns out that destroy/edit need show to be there as well because of resource.
I want to push show back into my application to fix this issue, can someone help me pinpoint the issue?
trashes_controller.rb
class TrashesController < ApplicationController
# GET /trashes
# GET /trashes.json
def index
#trash = Trash.all
#json = #trash.to_gmaps4rails
end
end
# GET /trashes/1
# GET /trashes/1.json
def show
#trash = Trash.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/new
# GET /trashes/new.json
def new
#trash = Trash.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #trash }
end
end
# GET /trashes/1/edit
def edit
#trash = Trash.find(params[:id])
end
# POST /trashes
# POST /trashes.json
def create
#trash = Trash.new(params[:trash])
respond_to do |format|
if #trash.save
format.html { redirect_to #trash, notice: 'Trash was successfully created.' }
format.json { render json: #trash, status: :created, location: #trash }
else
format.html { render action: "new" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# PUT /trashes/1
# PUT /trashes/1.json
def update
#trash = Trash.find(params[:id])
respond_to do |format|
if #trash.update_attributes(params[:trash])
format.html { redirect_to #trash, notice: 'Trash was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #trash.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trashes/1
# DELETE /trashes/1.json
def destroy
#trash = Trash.find(params[:id])
#trash.destroy
respond_to do |format|
format.html { redirect_to trashes_url }
format.json { head :no_content }
end
end
index.html.erb within trashes folder
<h1>Listing Boston/Cambridge trash bin locations</h1>
<%= gmaps4rails(#json) %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
<% #trash.each do |trash| %>
<tr>
<td><%= trash.name %></td>
<td><%= trash.address %></td>
<td><%= trash.category %></td>
<td><%= link_to 'Show', trash %></td>
<td><%= link_to 'Edit', edit_trash_path(trash) %></td>
<td><%= link_to 'Destroy', trash, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #trash.name %>
</p>
<p>
<b>Address:</b>
<%= #trash.address %>
</p>
<p>
<b>Category:</b>
<%= #trash.category %>
</p>
<%= link_to 'Edit', edit_trash_path(#trash) %> |
<%= link_to 'Back', trashes_path %>
I keep on getting undefined method `name' for nil:NilClass when I click show in my index root page in local host.
Here is my rake routes pages, probably more useful.
trashes GET /trashes(.:format) trashes#index
POST /trashes(.:format) trashes#create
new_trash GET /trashes/new(.:format) trashes#new
edit_trash GET /trashes/:id/edit(.:format) trashes#edit
trash GET /trashes/:id(.:format) trashes#show
PUT /trashes/:id(.:format) trashes#update
DELETE /trashes/:id(.:format) trashes#destroy
root / trashes#index
page GET /pages/*id high_voltage/pages#show
If your error occur when you try to click 'show' link, than modify your link_to path like this :
td><%= link_to 'Show', trash_path(trash) %></td>
Hope this will help.

I do not understand this Rails SyntaxError

I'm just beginning to work through Agile Web Development with Rails, 4th Edition, and I've run into a SyntaxError while setting up my first application in Chapter 6. The code is below as well as the error. I'm running Ruby 1.9.3 and Rails 3.2.1.3. I tried to follow the book exactly, so I'm not sure where I'm going wrong. I saw a similar error on this question but it did not fix my problem.
Here is the index.html for Product
<h1>Listing products</h1>
<table>
<tr>
<th>\</th>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #products.each do |product| %>
<tr>
<td><%= product.\ %></td>
<td><%= product.title %></td>
<td><%= product.description %></td>
<td><%= product.image_url %></td>
<td><%= product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
And here is the error that I'm getting
SyntaxError in Products#index
Showing C:/Sites/work/depot/app/views/products/index.html.erb where line #17 raised:
C:/Sites/work/depot/app/views/products/index.html.erb:17: syntax error, unexpected $undefined
...tput_buffer.append= ( product.\ );#output_buffer.safe_concat...
... ^
C:/Sites/work/depot/app/views/products/index.html.erb:26: syntax error, unexpected keyword_end, expecting ')'
'); end
^
C:/Sites/work/depot/app/views/products/index.html.erb:33: syntax error, unexpected keyword_ensure, expecting ')'
C:/Sites/work/depot/app/views/products/index.html.erb:35: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #17):
14:
15: <% #products.each do |product| %>
16: <tr>
17: <td><%= product.\ %></td>
18: <td><%= product.title %></td>
19: <td><%= product.description %></td>
20: <td><%= product.image_url %></td>
Trace of template inclusion: app/views/products/index.html.erb
Rails.root: C:/Sites/work/depot
Update to add the model file for Product and the error for removing the <%= product.\ %> lines per comments below.
class Product < ActiveRecord::Base
attr_accessible :\, :description, :image_url, :price, :title
end
NoMethodError in Products#index
Showing C:/Sites/work/depot/app/views/products/index.html.erb where line #15 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #15):
12: <th></th>
13: </tr>
14:
15: <% #products.each do |product| %>
16: <tr>
17:
18: <td><%= product.title %></td>
Rails.root: C:/Sites/work/depot
Further edited to add Controller file
class ProductsController < ApplicationController
# GET /products
# GET /products.json
def index
#products = Product.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #products }
end
end
# GET /products/1
# GET /products/1.json
def show
#product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #product }
end
end
# GET /products/new
# GET /products/new.json
def new
#product = Product.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #product }
end
end
# GET /products/1/edit
def edit
#product = Product.find(params[:id])
end
# POST /products
# POST /products.json
def create
#product = Product.new(params[:product])
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render json: #product, status: :created, location: #product }
else
format.html { render action: "new" }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.json
def update
#product = Product.find(params[:id])
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product = Product.find(params[:id])
#product.destroy
respond_to do |format|
format.html { redirect_to products_url }
format.json { head :no_content }
end
end
end
modify #products.each to #product.each. And delete line of "\".

Resources