NoMethodError in Pages#new
Showing /Users/janicelloyd/Sites/assignment/app/views/pages/_form.html.erb where line #27 raised:
undefined method `author_id' for #<Page:0x10532f018>
Extracted source (around line #27):
24: <%= f.text_area :content %>
25: </div>
26: <div class ="field">
27: <%= f.collection_select (:author_id, Author.all, :id, :name, :prompt => 'Select') %>
28: </div>
29: <div class="actions">
30: <%= f.submit %>
我收到了这个错误,我已经通过rake路线查看并且有作者ID。我试图通过ll控制器和数据库文件夹检查它所说的页面,但找不到问题。 希望有人可以提供帮助,如果您需要更多信息,请告诉我。 提前感谢您的帮助。
我添加了以下内容
我已经完成了rails生成迁移它仍然不能正常工作我的控制器看起来像这样:页面控制器
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
# GET /authors
# GET /authors.xml
def index
@authors = Author.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @authors }
end
end
# GET /authors/1
# GET /authors/1.xml
def show
@author = Author.find(params[:id])
@pages = @author.pages
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @author }
end
end
# GET /authors/new
# GET /authors/new.xml
def new
@author = Author.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @author }
end
end
# GET /authors/1/edit
def edit
@author = Author.find(params[:id])
end
# POST /authors
# POST /authors.xml
def create
@author = Author.new(params[:author])
respond_to do |format|
if @author.save
format.html { redirect_to(@author, :notice => 'Author was successfully created.') }
format.xml { render :xml => @author, :status => :created, :location => @author }
else
format.html { render :action => "new" }
format.xml { render :xml => @author.errors, :status => :unprocessable_entity }
end
end
end
# PUT /authors/1
# PUT /authors/1.xml
def update
@author = Author.find(params[:id])
respond_to do |format|
if @author.update_attributes(params[:author])
format.html { redirect_to(@author, :notice => 'Author was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @author.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /authors/1
# DELETE /authors/1.xml
def destroy
@author = Author.find(params[:id])
@author.destroy
respond_to do |format|
format.html { redirect_to(authors_url) }
format.xml { head :ok }
end
end
def author_id
@author = @pages.author
end
end
author.rb
class Author < ActiveRecord::Base
has_many :pages
end
page.rb
class Page < ActiveRecord::Base
belongs_to :author
end
迁移
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.string :title
t.string :excerpt
t.string :content
t.timestamps
end
end
def self.down
drop_table :pages
end
end
class CreateAuthors < ActiveRecord::Migration
def self.up
create_table :authors do |t|
t.integer :author_id
t.string :name
t.string :email
t.timestamps
end
end
def self.down
drop_table :authors
end
end
class AddAuthorIdToAuthors < ActiveRecord::Migration
def self.up
change_table :authors do |t|
t.references :author_id
end
end
def self.down
remove_column :authors, :author_id
end
end
end
class AddAuthorToPages < ActiveRecord::Migration
def self.up
change_table :pages do |t|
t.references :author_id
end
end
def self.down
remove_column :pages, :author_id
end
end
end
答案 0 :(得分:2)
这意味着您构建表单的对象没有名为author_id
的属性:这是正确的吗?
[编辑:看到迁移后]
您好Janice,您应该按如下方式编写迁移:
self.up do
change_table :pages do |t|
t.references :author
end
end
根本不应将 添加到authors
表中。
答案 1 :(得分:0)
在这里看一下纪录片:
我认为你没有设置:post(或其他)实例变量。可能需要:
<%= f.collection_select (:post, :author_id, Author.all, :id, :name, :prompt => 'Select') %> 28: 29: 30: <%= f.submit %>