1
OK、ここに私のRSpecのコードだ...のRuby/Railsの/ RSpecの - ActiveRecordの:: AssociationTypeMismatch:
before(:each) do
@attr = { :greeting => "Lorem ipsum", :recipient => @recipient }
end
it "should redirect to the home page" do
puts "spec: @attr = #{@attr}"
puts "spec: recipient = #{@attr[:recipient]}"
post :create, :card => @attr
response.should redirect_to(root_path)
end
さて、これからの出力は次のとおりです。
spec: @attr = {:greeting=>"Lorem ipsum", :recipient=>#<User id: 2, first_name: "Example", last_name: "User", email: "[email protected]", created_at: "2011-12-22 04:01:02", updated_at: "2011-12-22 04:01:02", encrypted_password: "2d1323ad5eb21fb5ae5e87dfa78a63b521c56833189cc049ee2...", salt: "2679fcc29a30e939541cb98cb65d1d508035fea0eff1136037a...", admin: false>}
spec: recipient = #<User:0xac5d80c>
だから我々は、その受信者を見ることができますユーザーです。
コントローラ側では、我々が持っている見...
def create
puts "create: Params = #{params}"
@card = current_user.sent_cards.build(params[:card])
if @card.save
flash[:success] = "Card created!"
redirect_to root_path
else
render 'pages/home'
end
end
create: Params = {"card"=>{"greeting"=>"Lorem ipsum", "recipient"=>"2"}, "controller"=>"cards", "action"=>"create"}
...の表示では、私は
1) CardsController POST 'create' success should create a card
Failure/Error: post :create, :card => @attr
ActiveRecord::AssociationTypeMismatch:
User(#90303150) expected, got String(#76171330)
# ./app/controllers/cards_controller.rb:7:in `create'
# ./spec/controllers/cards_controller_spec.rb:47:in `block (5 levels) in <top (required)>'
# ./spec/controllers/cards_controller_spec.rb:44:in `block (4 levels) in <top (required)>'
...のエラーを参照してください
Userオブジェクトはどのようにidに文字列として変更されましたか?何か案は?
ありがとうございました。 (各)私は前に ...のように見えるように、私の前に変更さ のattr =行い{:、=> "Loremのイプサム" の挨拶:recipient_id => recipient.id} エンド をあなたが何を意味するかということですか?それは私が見ていたエラーを取り除きましたが、今は... カード= current_user.sent_cards.build(params [:card]) (カード、attr、およびrecipient_idは "at" )は、作成されたカード内でrecipient_idがnilであることを示しています。カードを作成する前に、recipient_idからユーザーを取得する必要がありますか?他に何か? – slabounty
それを考え出した。別の問題。 find_by_idを使って受信者を取得しなければならなかった。 もう一度ありがとうございます。 – slabounty