Noob question that took me a whole day

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

What does Ratings extend from? Where is this file? What code is in the
new and create actions? What are you trying to do?

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

Can you show me the full ratings controller please? I’m especially
interested in the first few lines.

Ryan B. 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!

I see what you’re trying to do. A tutorial I wrote on restful routing
should be able to help you out with this:

Ryan B. wrote:

I see what you’re trying to do. A tutorial I wrote on restful routing
should be able to help you out with this:
frozenplague.net

thank you i will try it.

Kane Wu wrote:

Ryan B. wrote:

I see what you’re trying to do. A tutorial I wrote on restful routing
should be able to help you out with this:
frozenplague.net

thank you i will try it.

Hey I am still having trouble with the creating part. Now I got it to
say /wines/62/ratings/new but then when I try to create it goes back to
ratings/6 with the wine_id being nothing. Any idea why?

You must use nested routes. Try modifying your config/routes.rb like
this:

map.resources :wines do |wine|
wine.resources :ratings
end

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.