0

新しいピンを追加しようとしてもできません。代わりに、次のエラーが表示されます。

NoMethodError in PinsController#create

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

アプリケーション トレース | フレームワーク トレース | 完全なトレース

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

リクエスト

パラメーター:

{"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

アプリ/モデル/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

ルート.rb

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

 devise_for :users

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

アプリ/モデル/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

デシベル/移行/create_pins

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

      t.timestamps
    end
  end
end

デシベル/移行/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

デシベル/移行/add_name_to_users.rb

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

何がうまくいかなかったのかについてのアイデアはありますか?

関連性があるかどうかはわかりませんが、これは以前は機能していました。Mattan Griffel の One Month Rails コース -- Add Assoc bt Pins and Users ビデオを 29 分 50 秒までたどることができましたが、単純なフォームを追加するのを忘れていたため、Devise のカスタマイズに戻る必要があることに気付きました。

シンプルなフォームが追加されたので、先に進もうとしています-そしてここで立ち往生しています:(

更新:ピンを作成し、ピンにユーザー ID を追加するために、移行のやり直しを実行しました。次に、検証名の行を削除しました。ピンを作成すると、次のエラーが表示されます

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

助けてくれてありがとう

デシベル/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
4

1 に答える 1

3

:nameモデル内のの存在を検証していますが、フィールドPinがありません。:nameあなたUserが持っています。

validates :name, presence: trueモデルから を削除するだけですPin(5 行目)。

モデルを保存しようとすると、Rails はPinすべての検証を実行します。でプレゼンス検証が発生すると、が空白でない:nameかどうかがチェックされます。@pin.nameしかし問題は、Pinモデルに name メソッドがないことです。したがって、このエラーが発生します。

実際にPinモデルに が必要な場合は、それをテーブルnameに追加します。pins

$ rails g migration add_name_to_pins name:string
$ rake db:migrate
于 2013-09-01T23:19:26.487 に答える