Rails (One Month Rails) NoMethodError in PinsController#create - can't add new pin

1.1k Views Asked by At

When I try to add a new pin, I can't. Instead, I get this error:

NoMethodError in PinsController#create

undefined method `name' for #<pin:0x000001011b9638>

Application Trace | Framework Trace | Full Trace

app/controllers/pins_controller.rb:48:in `block in create'
app/controllers/pins_controller.rb:47:in `create'

Request

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"n9E2nob/KBzu20PEzYoQWXnibAUR5TH6iPWNd66383k=",
"pin"=>{"description"=>"stea"},
"commit"=>"Create Pin"}

pins_controller.rb

def create

@pin = current_user.pins.new(params[:pin])

respond_to do |format|
  if @pin.save
    format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
    format.json { render json: @pin, status: :created, location: @pin }
  else
    format.html { render action: "new" }
    format.json { render json: @pin.errors, status: :unprocessable_entity }
  end
 end
end

app/model/user.rb

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
     :rememberable, :trackable, :validatable

attr_accessible :email, :password, :password_confirmation, :remember_me, :name

has_many :pins, :dependent => :destroy
end

routes.rb

Omrails::Application.routes.draw do
 resources :pins

 devise_for :users

 root :to => 'pages#home'
 get 'about' => 'pages#about'

app/models/pin.rb

class Pin < ActiveRecord::Base
  attr_accessible :description

  validates :description, presence: true
    validates :name, presence: true

  belongs_to :user
    validates :user_id, presence: true
 end

db/migrate/create_pins

class CreatePins < ActiveRecord::Migration
  def change
    create_table :pins do |t|
      t.string :description

      t.timestamps
    end
  end
end

db/migrate/add_user_id_to_pins.rb

class AddUserIdToPins < ActiveRecord::Migration
  def change
    add_column :pins, :user_id, :integer
    add_index :pins, :user_id
  end
end

db/migrate/add_name_to_users.rb

class AddNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
  end
end

Any ideas about what has gone wrong?

Not sure if it's relevant, but this used to work. I was able to follow along Mattan Griffel's One Month Rails course -- Add Assoc bt Pins and Users video until 29m50s but then I realized that I had to skip back to Customizing Devise bec I forgot to add simple forms.

Now that simple forms have been added, I am trying to go forward - and getting stuck here :(

UPDATE: I ran migrate redo for creating pins and adding user id to pins. Then I removed the validate name line. Now I get the following error when I create pin

ActiveRecord::UnknownAttributeError in PinsController#new

unknown attribute: user_id 
app/controllers/pins_controller.rb:29:in `new'

pins_controller.rb

  def new
    @pin = current_user.pins.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @pin }
    end
  end

Many thanks for helping

db/schema.rb

ActiveRecord::Schema.define(:version => 20130828163738) do

  create_table "pins", :force => true do |t|
    t.string   "description"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name =>     "index_users_on_reset_password_token", :unique => true

end
1

There are 1 best solutions below

7
On

You are validating the presence of :name in the Pin model but it does not have a :name field. Your User has.

Just remove the validates :name, presence: true from you Pin model (line 5).

What happened is that Rails, when trying to save your Pin model, will run all the validations. When it encounters the presence validation on :name, it will check to see if @pin.name isn't blank. But the thing is that your Pin model does not have a name method. So it raises this error.

If you actually want your Pin model to have a name, add it the to pins table:

$ rails g migration add_name_to_pins name:string
$ rake db:migrate