开发者

uninitialized constant model1::model2 when trying to retrieve child records

I have two models, one is for pages and one is for authors.

I have nearly got it to show which author belongs to the pages but keep getting an error, saying:

uninitialized constant Page::Authors

I have defined a method for Authors in my pages controller @pages = @author.pages and not sure if it is right. hear is my code for my controllers.

class PagesController < ApplicationController
  def index
    @pages = Page.find(:all, :order => 'created_at DESC')
  end

  def show
    @page = Page.find(params[:id])
  end

  def new
    @page = Page.new
  end

  def edit
    @page = Page.find(params[:id])
  end

  def create
    @page = Page.new(params[:page])

      if @page.save
          redirect_to(@page, :notice => 'Page was successfully created.')
      else
          render :action => "new"
      end
  end

  def update
    @page = Page.find(params[:id])

      if @page.update_attributes(params[:page])
        redirect_to(@page, :notice => 'Page was successfully updated.')
      else
        render :action => "edit"
      end
  end

  def destroy
    @page = Page.find(params[:id])
    @page.destroy
  end

  def author
   @pages = @author.pages
  end
end

And here is my Authors controller:

class AuthorsController < ApplicationController
  def index
    @authors = Author.find(:all, :order => 'created_at DESC')
  end

  def show
    @author = Author.find(params[:id])
  end

  def new
    @author = Author.new
  end

  def edit
    @author = Author.find(params[:id])
  end

  def create
    @author = Author.new(params[:author])

      if @author.save
        redirect_to(@author, :notice => 'Author was successfully created.') 
      else
        render :action => "new" 
    end
  end

  def update
    @author = Author.find(params[:id])

      if @author.update_attributes(params[:author])
       redirect_to(@author, :notice => 'Author was successfully updated.') 
      else
        render :action => "edit" 
    end
  end

  def destroy
    @author = Author.find(params[:id])
    @author.destroy
  end

  def author_id
    @author = @pages.author
  end
end

I just want to display who has created the pages, I am using a select from the form to get the author which has already been entered 开发者_开发知识库into the database. When you create a new page the select list is selected and the content is entered and then when you show the new or show template that is when the error occurs.

In my models I have said belongs_to :author and has_many :pages.

I am close.

Any ideas would be great. thanks


What's missing here is the stack-trace that shows where the line in question is exercised. It is common to make the mistake of referencing the Authors class instead of the actual Author and end up with errors like this. Nothing in what you've pasted seems to be problematic in that way.

If Author has_many :pages and Page belongs_to :author then this should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜