尝试检索子记录时未初始化的常量model1 :: model2(uninitialized constant model1::model2 when trying to retrieve child

编程入门 行业动态 更新时间:2024-10-23 13:22:49
尝试检索子记录时未初始化的常量model1 :: model2(uninitialized constant model1::model2 when trying to retrieve child records)

我有两个模型,一个是页面,一个是作者。

我几乎得到它来显示哪个作者属于页面,但一直收到错误,说:

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::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

最满意答案

这里缺少的是堆栈跟踪,它显示了相关行的运行位置。 通常会错误地引用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.

更多推荐

本文发布于:2023-08-04 16:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   初始化   uninitialized   retrieve   child

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!