views:

172

answers:

2

I created a basic scaffold for a Foo model with a single property - bar:String

foos/new.html.erb:

<h1>New foo</h1>

<% form_for(@foo) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :bar %><br />
    <%= f.text_field :bar %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', foos_path %>

But I get a NoMethodError for my 'bar' property:

 NoMethodError in Foos#new

Showing app/views/foos/new.html.erb where line #8 raised:

undefined method `bar' for #<Foo id: nil, created_at: nil, updated_at: nil>

Extracted source (around line #8):

5: 
6:   <p>
7:     <%= f.label :bar %><br />
8:     <%= f.text_field :bar %>
9:   </p>
10:   <p>
11:     <%= f.submit 'Create' %>

As you can see, bar the only property in my Foo model:

class CreateFoos < ActiveRecord::Migration
  def self.up
    create_table :foos do |t|
      t.String :bar

      t.timestamps
    end
  end

  def self.down
    drop_table :foos
  end
end

And the new method in the foos_controller is from the default scaffold:

  def new
    @foo = Foo.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @foo }
    end
  end

Any ideas would be appreciated

+1  A: 

It would seem you capitalized the word String in your db migration. Try it with a lower case s and force the migration to run again (rake db:migrate:redo, assuming that this was your latest migration, otherwise use

 rake db:migrate:down VERSION=29843923 && rake db:migrate:up VERSION=29843923

where 29843923 is your migration timestamp.).

cwninja
Tried that but when I checked the database there was no bar column. Think I'm going to delete this project and start over.
toma
A: 

Did you executed your migration?

Lichtamberg