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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Ahmy Yulrizka at Mar 27, 2012 at 12:23 pm
    Check the params[:user_id] i think it was nil

    Ahmy Yulrizka

    On Tue, Mar 27, 2012 at 7:19 PM, Neil Bye 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Neil Bye at Mar 27, 2012 at 12:48 pm

    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Ahmy Yulrizka at Mar 27, 2012 at 3:11 pm
    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 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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Neil Bye at Mar 27, 2012 at 4:17 pm

    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Ahmy Yulrizka at Mar 28, 2012 at 5:18 am
    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 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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Neil Bye at Mar 28, 2012 at 7:30 pm

    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
    This 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 27, '12 at 12:19p
activeMar 28, '12 at 7:30p
posts7
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Neil Bye: 4 posts Ahmy Yulrizka: 3 posts

People

Translate

site design / logo © 2021 Grokbase