我有两个模型,一个是页面,一个是作者。
我几乎得到它来显示哪个作者属于页面,但一直收到错误,说:
uninitialized constant Page::Authors我在我的页面控制器@pages = @author.pages为作者定义了一个方法,但不确定它是否正确。 听到我的控制器的代码。
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这是我的作者控制器:
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我只想显示谁创建了页面,我使用表单中的选择来获取已经输入数据库的作者。 创建新页面时,将选择选择列表并输入内容,然后在显示错误发生时的新模板或显示模板时。
在我的模型中,我说过belongs_to:author和has_many:pages。
我很亲密
任何想法都会很棒。 谢谢
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::AuthorsI 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 endAnd 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 endI 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
最满意答案
这里缺少的是堆栈跟踪,它显示了相关行的运行位置。 通常会错误地引用Authors类而不是实际的Author ,最终会出现这样的错误。 你粘贴的东西似乎没有任何问题。
如果Author has_many :pages和Page belongs_to :author那么这应该有效。
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.
更多推荐
发布评论