I'm building a Task Manager in Rails. I used scope to get all instances of a Task model that are due today. The scope on the Task model works and is returning only Tasks due today when calling due_today on Tasks in the Rails console. But I can't seem to show these Tasks due today in the view. Do I need to add a conditional?
This is my views index.html.erb
<p id="notice"><%= notice %></p>
<h1>Team's Tasks</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></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 %>
</tbody>
</table>
<br>
<h1>Tasks due today.</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></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 %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task', new_task_path %>
</p>
this is my Task model
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue, -> { where("duedate < ?", Time.now) }
#use scope on Task model to get only tasks that are due today.
scope :due_today, ->{ where("duedate >= ? AND duedate <= ?", Date.current.beginning_of_day, Date.current.end_of_day) }
end
This is the controller
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
# GET /tasks or /tasks.json
# Order all tasks by ascending order
def index
#tasks = Task.all.order(duedate: :asc)
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: "Task was successfully created." }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title, :description, :duedate, :completed)
end
end
I assume you have a TasksController and an index method there.
try following in the index method
def index
#tasks = Task.due_today
end
let me know if this works
You need to change due_date scope.. use between or date_trunc method.
where("date_trunc('day', duedate)::date = ?", Date.today)
let me know if it works
Got it to work. No changes to the controller. None needed. This is the code.
This is the Task model.
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue, -> { where("duedate < ?", Time.now) }
#use scope on Task model to get Tasks due today.
scope :due_today, ->{ where("duedate >= ? AND duedate <= ?", Date.current.beginning_of_day, Date.current.end_of_day) }
def overdue?
duedate < Time.now
end
def due_today?
duedate >= Date.current.beginning_of_day && duedate <= Date.current.end_of_day
end
end
This is the Task view.
<p id="notice"><%= notice %></p>
<h1>Team Task Manager</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></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 %>
</tbody>
</table>
<br>
<br>
<h1>DUE TODAY!</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></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 %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task', new_task_path %>
</p>
Related
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
Actually I would like to hide all the columns from a table in rails view, if those columns contains no data i.e., zero.
Secondly, I want to delete the respective row completely if quantity == 0.
Find the screenshot for better understanding as below;
screenshot.png
index.html.erb
<div class="col-md-10 col-md-offset-1">
<div class="table-responsive myTable">
<table class="table listing text-center">
<tr class="tr-head">
<td>DESCRIPTION</td>
<td>COLOR</td>
<td>QUANTITY</td>
<td>RETAIL PRICE</td>
<td>TOTAL AMOUNT</td>
<td>CARTON NO</td>
<td>CUSTOMER 1</td>
<td>CUSTOMER 2</td>
<td>ACTUAL QUANTITY</td>
</tr>
<% #purchases.each do |purchase| %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-2"><%= purchase.description %></td>
<td class="col-1"><%= purchase.color %></td>
<td class="col-2"><%= purchase.quantity %></td>
<td class="col-2"><%= number_with_precision(purchase.rprice, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= number_with_precision(purchase.tamount, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= purchase.cartonno %></td>
<td class="col-2"><%= purchase.cus1 %></td>
<td class="col-2"><%= purchase.cus2 %></td>
<td class="col-2"><%= tquantity = purchase.quantity - purchase.cus1 - purchase.cus2 %></td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
purchases_controller.rb
class PurchasesController < ApplicationController
before_action :set_purchase, only: [:show, :edit, :update, :destroy]
# GET /Stockings
# GET /deldetails.json
def index
##purchases = Purchase.all
#purchases = Purchase.where("tquantity !=?", 0)
end
def import
Purchase.import(params[:file])
redirect_to purchases_url, notice: "Purchases imported."
end
# GET /purchases/1
# GET /purchases/1.json
def show
end
# GET /purchases/new
def new
#purchase = Purchase.new
end
# GET /purchases/1/edit
def edit
end
# POST /purchases
# POST /purchases.json
def create
#purchase = Purchase.new(purchase_params)
respond_to do |format|
if #purchase.save
format.html { redirect_to #purchase, notice: 'Purchase was successfully created.' }
format.json { render :show, status: :created, location: #purchase }
else
format.html { render :new }
format.json { render json: #purchase.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /purchases/1
# PATCH/PUT /purchases/1.json
def update
respond_to do |format|
if #purchase.update(purchase_params)
format.html { redirect_to #purchase, notice: 'Purchase was successfully updated.' }
format.json { render :show, status: :ok, location: #purchase }
else
format.html { render :edit }
format.json { render json: #purchase.errors, status: :unprocessable_entity }
end
end
end
# DELETE /purchases/1
# DELETE /purchases/1.json
def destroy
#purchase.destroy
respond_to do |format|
format.html { redirect_to purchases_url, notice: 'Purchase was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_purchase
#purchase = Purchase.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def purchase_params
params.require(:purchase).permit(:season, :category, :articleno, :description, :color, :quantity, :rprice, :tamount, :cartonno, :cus1, :cus2, :tquantity )
end
end
purchase.rb
class Purchase < ActiveRecord::Base
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Purchase.create! row.to_hash
end
end
end
Do I need to go for if else or ternary operator for checking the for the columns if it is empty or not ?
Any suggestions are most welcome.
Thank you in advance.
you can add a condition in your view.
<% unless #purchases.collect{|p| p.cus2}.uniq == [0] %>
<td>CUSTOMER 2</td>
<% end %>
and
<% #purchases.each do |purchase| %>
<% unless purchase.quantity == 0 %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-2"><%= purchase.description %></td>
<td class="col-1"><%= purchase.color %></td>
<td class="col-2"><%= purchase.quantity %></td>
<td class="col-2"><%= number_with_precision(purchase.rprice, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= number_with_precision(purchase.tamount, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= purchase.cartonno %></td>
<td class="col-2"><%= purchase.cus1 %></td>
<% unless #purchases.collect{|p| p.cus2}.uniq == [0] %>
<td class="col-2"><%= purchase.cus2 %></td>
<% end %>
<td class="col-2"><%= tquantity = purchase.quantity - purchase.cus1 - purchase.cus2 %></td>
</tr>
<% end %>
<% end %>
and to remove row with 0 quantity the best thing is do not select those records having 0 quantity with in query.
#purchases = Purchase.where("quantity !=?", 0)
or alternatively I also have updated the view code which is less recommended.
I have understand your problem. But why are you taking those records inside the instance varible #purchases.
You should write a query or scope like so that will exclude the record which have the values zero inside the column.
Lets have a example like as below...
Class Purchase < ActiveRecord::Base
scope :exclude_zero_value_customers, -> { where('cus1 <> ? && cus2 <> ?', 0,0) }
end
Or you can hide it directly from the view as well, But in that case if you have thousands of records then also it will go one time with that loop as bwlow...
<% #purchases.each do |purchase| %>
<% if !purchase.cus1.zero? && !purchase.cus2.zero? %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-2"><%= purchase.description %></td>
<td class="col-1"><%= purchase.color %></td>
<td class="col-2"><%= purchase.quantity %></td>
<td class="col-2"><%= number_with_precision(purchase.rprice, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= number_with_precision(purchase.tamount, :delimiter => ",", :precision => 2) %></td>
<td class="col-2"><%= purchase.cartonno %></td>
<td class="col-2"><%= purchase.cus1 %></td>
<td class="col-2"><%= purchase.cus2 %></td>
<td class="col-2"><%= tquantity = purchase.quantity - purchase.cus1 - purchase.cus2 %></td>
</tr>
<% end %>
<% end %>
I have a question:
#order_item = #order.order_items.find_or_initialize_by(product_id: params[:product_id])
I want to add one to the order item’s quantity before the .save is called.
The default value of the quantity is 0. Here is my complete code:
def create
#order_item = #order.order_items.find_or_initialize_by(product_id: params[:product_id])
respond_to do |format|
if #order_item.save
format.html { redirect_to #order, notice: 'Successfully added product to cart.' }
format.json { render :show, status: :created, location: #order_item }
else
format.html { render :new }
format.json { render json: #order_item.errors, status: :unprocessable_entity }
end
end
end
Im new to ruby and rails. How to do that? Thank you
UPDATE
<tr>
<th>Items:</th>
<td><%= #order.order_items.count %></td>
</tr>
<tr>
<th>Items</th>
<th>Title</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Subtotal</th>
<th></th>
</tr>
<% #order.order_items.each do |item| %>
<tr>
<td><%= image_tag "products/#{item.product.image_url}" %></td>
<td><%= item.product.title %></td>
<td><%= item.quantity %></td>
<td><%= print_price item.product.price %></td>
<td><%= print_price item.subtotal %></td>
<td><%= link_to 'Remove', item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<tr>
<th>TOTAL</th>
<td></td>
<td></td>
<td></td>
<td><%= print_price #order.total %></td>
<td></td>
</tr>
My current code is only print out total order_items in some order, what if for example each order_item quantity is 2. So I want to print out all total quantity.
<%= #order.order_items.count %>
How to do that?
If you have quantity attribute in order_items table, you can do that by simply assigning value to the setter, before saving.
#order_item.quantity = 1
Update:
I assume, you want order items quantities sum.
You can find total order_items quantities by
#order.order_items.sum(:quantity)
Replace your
<th>Items:</th>
<td><%= #order.order_items.count %></td>
with
<th>Items:</th>
<td><%= #order.order_items.sum(:quantity) %></td>
I'm still a Rails-Learner and getting desperate about implementing an ajax live search. The search seems to work on submitting, but not on keyup. Can't figure out why...
index.html.erb
<%= form_tag contacts_path, :method => 'get', :id => "contacts_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="cresults_div"><%= render 'cresults' %></div>
<% end %>
<%= link_to 'New Contact', new_contact_path %>
contacts_controller.rb
def index
#contacts = Contact.search(params[:search])
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
index.js.erb
$("#cresults_div").html("<%= escape_javascript(render("cresults")) %>");
contact.rb
def self.search(search)
if search
where('last_name LIKE ?', "%#{search}%")
else
scoped
end
end
contacts.js.coffee
jQuery ->
# Ajax search on submit
$('#contacts_search').submit( ->
$.get(this.action, $(this).serialize(), null, 'script')
false
)
# Ajax search on keyup
$('#contacts_search input').keyup( ->
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, 'script')
false
)
_cresults.html.erb
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<h1>Listing contacts</h1>
<table>
<tr>
<th>Salutation</th>
<th>First name</th>
<th>Last name</th>
<th>Stree</th>
<th>Street no</th>
<th>Zip</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Phone</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #contacts.each do |contact| %>
<tr>
<td><%= contact.salutation %></td>
<td><%= contact.first_name %></td>
<td><%= contact.last_name %></td>
<td><%= contact.stree %></td>
<td><%= contact.street_no %></td>
<td><%= contact.zip %></td>
<td><%= contact.city %></td>
<td><%= contact.state %></td>
<td><%= contact.country %></td>
<td><%= contact.phone %></td>
<td><%= contact.email %></td>
<td><%= link_to 'Show', contact %></td>
<td><%= link_to 'Edit', edit_contact_path(contact) %></td>
<td><%= link_to 'Destroy', contact, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
also tried to additional add
application.js
$(function() {
$("#contacts_search input").keyup(function() {
$.get($("#contacts_search").attr("action"), $("#contacts_search").serialize(), null, "script");
return false;
});
});
But the live search won't start on typing... why?
In this particular case I had to remove the
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
Block from the index-Method in the contacts controller
as the title says I'm trying to list all cars of one specific user, also each car has two options show and update
as you see in the picture, my problem is when I want to show or edit the selected car, it routes (look the bottom left of the image) it takes all the ids of all the cars for example users/"id"/cars/"id1"/"id2" instead to take the id of the specific car: users/"id"/cars/"id1"/
here is te index.html.erb file:
<div class="container">
<h1>Listing Cars</h1>
<table class="table table-condensed">
<tr>
<th>Brand</th>
<th>Color</th>
<th>Model</th>
<th>Year</th>
<th></th>
<th></th>
</tr>
<% #car.each do |car| %>
<tr>
<td><%= car.brand %></td>
<td><%= car.color %></td>
<td><%= car.model %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', user_car_path(#user,#car) %></td>
<td><%= link_to 'Edit', edit_user_car_path(#user, #car) %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New car', new_user_car_path, :class => "btn btn-primary" %>
</div>
and whether you need the car controller:
class CarsController < ApplicationController
def new
#user = User.find(params[:user_id])
#car = #user.cars.build
end
def create
#user = User.find(params[:user_id])
#car = #user.cars.build(params[:car])
if #car.save
redirect_to user_car_path(#user, #car), :flash => { :notice => " car created!" }
else
redirect_to new_user_car_path ,:flash => { :notice => " sorry try again :(" }
end
end
def show
#user = User.find(params[:user_id])
#car = #user.cars.find(params[:id])
#redirect_to user_car_path(#user, #car)
end
def index
#user = User.find(params[:user_id])
#car = #user.cars.all
end
def edit
#user = User.find(params[:user_id])
#car = #user.cars.find(params[:id])
#redirect_to user_car_path(#user, #car)
end
def update
#user = User.find(params[:user_id])
#car = #user.cars.find(params[:id])
if #car.update_attributes(params[:car])
redirect_to user_cars_path, :flash => { :notice => " Car Updated!" }
else
render 'edit'
end
end
end
Inside a Ruby enumerator, the block variable is each member of the enumerator, one after the other. So if, in your sample code, #cars is ["Toyota", "Mazda", "Honda"], then car will be first "Toyota", then "Mazda", then "Honda".
This is a long way of saying you're using the instance variable when you should be using the block variable. ;) Correct your code to look like this:
<% #car.each do |car| %>
<tr>
<td><%= car.brand %></td>
<td><%= car.color %></td>
<td><%= car.model %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', user_car_path(#user, car) %></td>
<td><%= link_to 'Edit', edit_user_car_path(#user, car) %></td>
</tr>
<% end %>
It should be car, not #car, in your routes.