Janice Lloyd
Janice Lloyd

Reputation: 1

No method error in Rails

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

i am getting this error i have looked through with rake routes and there is author id. I have tried to check through ll controllers and db folders and the page it says but can't find the problem. Hope someone can help, if you need more info please let me know. thank you in advance for any help.

I have added the following

i have done rails generate migration it still isn't working my controllers look like this : pages controller

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 my authors controller

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

migrations

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

Upvotes: 0

Views: 2641

Answers (2)

nathanvda
nathanvda

Reputation: 50057

This would mean that the object you are building the form for, does not have an attribute called author_id: is that correct?

[edit: after seeing the migration]

Hi Janice, you should write the migration as follows:

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

This should not be added to the authors table at all.

Upvotes: 2

Spyros
Spyros

Reputation: 48706

Taking a look at the documentary here :

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

I think that you are have not set the :post(or whatever) instance variable. It would probably need to be :

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

Upvotes: 0

Related Questions