开发者

Sqlite on rails problem with relations

开发者 https://www.devze.com 2023-01-10 11:37 出处:网络
I have a problem with relations while using sqlite3 on rails. First i build my scaffolds, add the references to migration files,

I have a problem with relations while using sqlite3 on rails.

First i build my scaffolds, add the references to migration files, add belongs_to has_many to models than get my database up and runinig with basic rake db:migrate command.

And then it doesn't work, I guess there is a missing step which i cannot figure out :S

By the way i am tryng to implement the example talewiki on Building Dynamic Web 2.0 Websites with Ruby on Rails, i am at page 75. The example is on mysql.

class GenresController < ApplicationController
  # GET /genres
  # GET /genres.xml
  def index
    @genres = Genre.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @genres }
    end
  end

  # GET /genres/1
  # GET /genres/1.xml
  def show
    @genre = Genre.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @genre }
    end
  end

  # GET /genres/new
  # GET /genres/new.xml
  def new
    @genre = Genre.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @genre }
    end
  end

  # GET /genres/1/edit
  def edit
    @genre = Genre.find(params[:id])
  end

  # POST /genres
  # POST /genres.xml
  def create
    @genre = Genre.new(params[:genre])

    respond_to do |format|
      if @genre.save
        format.html { redirect_to(@genre, :notice => 'Genre was successfully created.') }
        format.xml  { render :xml => @genre, :status => :created, :location => @genre }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @genre.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /genres/1
  # PUT /genres/1.xml
  def update
    @genre = Genre.find(params[:id])

    respond_to do |format|
      if @genre.update_attributes(params[:genre])
        format.html { redirect_to(@genre, :notice => 'Genre was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @genre.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /genres/1
  # DELETE /genres/1.xml
  def destroy
    @genre = Genre.find(params[:id])
    @genre.destroy

    respond_to do |format|
      format.html { redirect_to(开发者_开发问答genres_url) }
      format.xml  { head :ok }
    end
  end
end


The error is occurring on this line:

@genre = Genre.find(params[:id])

giving

ActiveRecord::RecordNotFound in GenresController#show Couldn't find Genre with ID=tales

That means that params[:id] has the value "tales" which is wrong. I'm guessing here, but I bet that somewhere in the form or elsewhere there is an attempt to do a GET on /genre/tales instead of /tales/genre/:id (where :id should be an integer). I'm also guessing you have a mapping in routes.rb like:

map.resources :tales, :has_many => :genres

I don't have a copy of the book you're following.

0

精彩评论

暂无评论...
验证码 换一张
取 消