2011-05-02 114 views
0
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 %> 

我得到這個錯誤,我已經看過耙路線和有作者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

請使用反引號\'這些\'或使用四個空間縮進,如果是多行格式化你的代碼。 – 2011-05-02 12:14:29

回答

0

考慮看看紀錄片在這裏:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

我認爲你是沒有設置:職位(或者其他)實例變量。它可能將需要:

<%= f.collection_select (:post, :author_id, Author.all, :id, :name, :prompt => 'Select') %> 28: 29: 30: <%= f.submit %> 
+0

@spyrosp你好:其實你的鏈接是指使用它獨立(不'form'對象)。如果以'f.collection_select'格式使用它,則該對象是已知的並被刪除。 – nathanvda 2011-05-02 09:56:45

+0

HI感謝您的幫助,我試圖寫帖子時,從下拉列表中選擇作者。當我添加額外的頁面時,它會提取語法錯誤和原始錯誤。 – 2011-05-02 09:56:56

+0

ohn nathanvda,我的壞,沒有注意到! – Spyros 2011-05-02 09:58:10

2

這將意味着你正在建設形式爲對象,沒有叫author_id屬性:是正確的?

[編輯:看到遷移後]

喜珍妮絲,你應該寫遷移如下:

self.up do 
    change_table :pages do |t| 
    t.references :author 
    end 
end 

這應該被添加到authors表都沒有。

+0

我認爲是什麼跟它是 – 2011-05-02 09:58:35

+0

關穎,那麼你就需要將其添加到您的遷移:) – Spyros 2011-05-02 10:02:17

+0

我已經做了鐵軌產生遷移仍無法工作 – 2011-05-02 12:06:42