11175
(-- --)
August 18, 2008, 7:03am
1
Alright I spent 3 hours on the easiest question in ruby on rails and
still can't figure out the answer. Please help... Basically I am trying
to create a new object. So of course I have two methods
class Ratings
def new
end
def create
end
Inside new I was already able to find the object that I want, a wine
object. Now how am I suppose to pass that wine object from the new
method to the create method? Thanks
radar
(Ryan Bigg)
August 18, 2008, 7:06am
2
What does Ratings extend from? Where is this file? What code is in the new and create actions? What are you trying to do?
11175
(-- --)
August 18, 2008, 7:10am
3
Ryan Bigg wrote:
What does Ratings extend from? Where is this file? What code is in the
new and create actions? What are you trying to do?
ratings extend from application, this file is in the controller folder?
def new
@rating = Rating.new
@wine = Wine.find(params[:id])
@rating.wine = @wine
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @rating }
end
end
def create
@rating = Rating.new(params[:rating])
@rating.wine = @wine
respond_to do |format|
if @rating.save
flash[:notice] = 'Rating was successfully created.'
format.html { redirect_to(@rating ) }
format.xml { render :xml => @rating , :status => :created,
:location => @rating }
else
format.html { render :action => "new" }
format.xml { render :xml => @rating.errors , :status =>
:unprocessable_entity }
end
end
end
I am trying to pass the @wine from new method to the create method
radar
(Ryan Bigg)
August 18, 2008, 7:18am
4
Can you show me the full ratings controller please? I'm especially interested in the first few lines.
11175
(-- --)
August 18, 2008, 7:25am
5
Ryan Bigg wrote:
Can you show me the full ratings controller please? I'm especially
interested in the first few lines.
class RatingsController < ApplicationController
layout 'standard'
# GET /ratings
# GET /ratings.xml
before_filter :login_required, :only => [ :destroy]
def index
@ratings = Rating.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @ratings }
end
end
# GET /ratings/1
# GET /ratings/1.xml
def show
@rating = Rating.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @rating }
end
end
# GET /ratings/new
# GET /ratings/new.xml
def new
@rating = Rating.new
@wine = Wine.find(params[:id])
@rating.wine = @wine
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @rating }
end
end
# GET /ratings/1/edit
def edit
@rating = Rating.find(params[:id])
end
# POST /ratings
# POST /ratings.xml
def create
@rating = Rating.new(params[:rating])
@wine = Wine.find(params[:wine])
@rating.wine = @wine
respond_to do |format|
if @rating.save
flash[:notice] = 'Rating was successfully created.'
format.html { redirect_to(@rating ) }
format.xml { render :xml => @rating , :status => :created,
:location => @rating }
else
format.html { render :action => "new" }
format.xml { render :xml => @rating.errors , :status =>
:unprocessable_entity }
end
end
end
# PUT /ratings/1
# PUT /ratings/1.xml
def update
@rating = Rating.find(params[:id])
@wine = Wine.find(params[:wine])
@rating.wine = @wine
respond_to do |format|
if @rating.update_attributes (params[:rating])
flash[:notice] = 'Rating was successfully updated.'
format.html { redirect_to(@rating ) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @rating.errors , :status =>
:unprocessable_entity }
end
end
end
# DELETE /ratings/1
# DELETE /ratings/1.xml
def destroy
@rating = Rating.find(params[:id])
@rating.destroy
respond_to do |format|
format.html { redirect_to(ratings_url) }
format.xml { head :ok }
end
end
end
Thanks!
david4
(david)
August 19, 2008, 7:22am
8
You must use nested routes. Try modifying your config/routes.rb like
this:
map.resources :wines do |wine|
wine.resources :ratings
end
radar
(Ryan Bigg)
August 19, 2008, 7:47am
9
I think he is using nested routes. His form is wrong.
Instead of: form_for @rating
It should be: form_for [@wine , @rating ]
It appears the guide doesn't go into this detail clearly enough, I will update it.