【发布时间】:2011-05-01 19:04:26
【问题描述】:
我有两种模型,一种用于页面,一种用于作者。
我几乎可以显示哪个作者属于页面,但不断收到错误消息:
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。
我很接近。
任何想法都会很棒。谢谢
【问题讨论】:
-
请注意,
<code>在 StackOverflow 上不起作用。而是缩进四个空格。
标签: ruby-on-rails ruby-on-rails-3