I am trying to run this test
setup do
@comment = comments(:hello)
@comment.commenter = "cleb"
@comment.body = "hello"
@comment.user_id = 1
end
test "should create comment" do
assert_difference(''Comment.count'') do
post :create, :comment => @comment.attributes
end
assert_redirected_to comment_path(assigns(:comment))
end
It comes up with
test_should_create_comment(CommentsControllerTest):
ActiveRecord::RecordNotFound: Couldn''t find User without an ID
This is from comments.yml
hello:
commenter: cleb
body: hello
user_id: 1
This is from comments.controller
def create
@cuser = @current_user
@user = User.find(params[:user_id])
@user.comments.create(:user_id => @user.id, :commenter =>
@cuser.login, :body => params[:comment][:body])
respond_to do |format|
format.js
format.html {redirect_to user_path}
end
end
What am I doing wrong?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Check the params[:user_id] i think it was nil Ahmy Yulrizka On Tue, Mar 27, 2012 at 7:19 PM, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @user = User.find(params[:user_id])-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Cpx Cpx wrote in post #1053539:> Check the params[:user_id] i think it was nil > > Ahmy YulrizkaSo it is. How can I introduce a user in the fixtures or setup to give it a value? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This line: post :create, :comment => @comment.attributes The second parameter (hash) is the content of params in your controller. So basically your params[:comment] would be @comment.attributes So if you expected params[:user_id] then you should pass it to the post call Ahmy Yulrizka On Tue, Mar 27, 2012 at 7:47 PM, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Cpx Cpx wrote in post #1053539: > > Check the params[:user_id] i think it was nil > > > > Ahmy Yulrizka > > > So it is. How can I introduce a user in the fixtures or setup to give it > a value? > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Cpx Cpx wrote in post #1053588:> This line: > post :create, :comment => @comment.attributes > > The second parameter (hash) is the content of params in your controller. > So > basically your params[:comment] would be @comment.attributes > > So if you expected params[:user_id] then you should pass it to the post > call > > Ahmy Yulrizka:user_id is one of the attributes setup do @comment = comments(:hello) @comment.commenter = "cleb" @comment.body = "hello" @comment.user_id = 1 end Neil -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
so basically its not params[:user_id] it''s params[:comment][:user_id] Ahmy Yulrizka On Tue, Mar 27, 2012 at 11:17 PM, Neil Bye <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Cpx Cpx wrote in post #1053588: > > This line: > > post :create, :comment => @comment.attributes > > > > The second parameter (hash) is the content of params in your controller. > > So > > basically your params[:comment] would be @comment.attributes > > > > So if you expected params[:user_id] then you should pass it to the post > > call > > > > Ahmy Yulrizka > > :user_id is one of the attributes > > setup do > @comment = comments(:hello) > @comment.commenter = "cleb" > @comment.body = "hello" > @comment.user_id = 1 > end > > Neil > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Cpx Cpx wrote in post #1053588:> This line: > post :create, :comment => @comment.attributes > > The second parameter (hash) is the content of params in your controller. > So > basically your params[:comment] would be @comment.attributes > > So if you expected params[:user_id] then you should pass it to the post > call > > Ahmy YulrizkaThis did it assert_difference(''Comment.count'') do post :create, :comment => @comment.attributes, :user_id => 2 end Neil -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.