i''m trying to add comments to a blog. I got it working, but i want to store the user too, so I added the user_id to the blogcomments table... Since then, the page renders - but the comment is no longer stored. (the database stores the user id, but not the body, blogpost id) def comment @blogcomment = Blogcomment.find(:all, :order => "created_at desc") @user = User.find(session[:user_id]) @blogcomment = Blogcomment.create(:id => params[:body], :user_id => session[:user_id]) ) flash[:notice] = "Added your comment" redirect_to :action => "show", :id => params[:id] end Can you see why the values are being set to NULL/ I''ve spent all evening trying different things, but no joy yet... Thanks -- 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.
On Sun, 11 Jul 2010, RubyonRails_newbie wrote:> i''m trying to add comments to a blog. > > I got it working, but i want to store the user too, so I added the > user_id to the blogcomments table... > > Since then, the page renders - but the comment is no longer stored. > > (the database stores the user id, but not the body, blogpost id) > > def comment > @blogcomment = Blogcomment.find(:all, :order => "created_at > desc") > @user = User.find(session[:user_id]) > > @blogcomment = Blogcomment.create(:id => > params[:body],Do you mean :body there, rather than :id? David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Stay tuned for next event announcement! Rubyist http://www.compleatrubyist.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.
I tried both :body and :id but the same issue: @blogcomment = Blogcomment.create(:id => params[:body], :user_id => session[:user_id]) On 11 July, 21:23, "David A. Black" <dbl...-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org> wrote:> On Sun, 11 Jul 2010, RubyonRails_newbie wrote: > > i''m trying to add comments to a blog. > > > I got it working, but i want to store the user too, so I added the > > user_id to the blogcomments table... > > > Since then, the page renders - but the comment is no longer stored. > > > (the database stores the user id, but not the body, blogpost id) > > > def comment > > @blogcomment = Blogcomment.find(:all, :order => "created_at > > desc") > > @user = User.find(session[:user_id]) > > > @blogcomment = Blogcomment.create(:id => > params[:body], > > Do you mean :body there, rather than :id? > > David > > -- > David A. Black, Senior Developer, Cyrus Innovation Inc. > > The Ruby training with Black/Brown/McAnally > Compleat Stay tuned for next event announcement! > Rubyist http://www.compleatrubyist.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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, 11 Jul 2010, RubyonRails_newbie wrote:> On 11 July, 21:23, "David A. Black" <dbl...-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org> wrote: >> On Sun, 11 Jul 2010, RubyonRails_newbie wrote: >>> i''m trying to add comments to a blog. >> >>> I got it working, but i want to store the user too, so I added the >>> user_id to the blogcomments table... >> >>> Since then, the page renders - but the comment is no longer stored. >> >>> (the database stores the user id, but not the body, blogpost id) >> >>> def comment >>> @blogcomment = Blogcomment.find(:all, :order => "created_at >>> desc") >>> @user = User.find(session[:user_id]) >> >>> @blogcomment = Blogcomment.create(:id => > params[:body], >> >> Do you mean :body there, rather than :id? >> > I tried both :body and :id but the same issue: > > @blogcomment = Blogcomment.create(:id => params[:body], :user_id => > session[:user_id])I don''t know the details of your app but it looks like you need to pass in the post id, the user id, and the (text) body of the comment -- something like: Blogcomment.create(:post_id => params[:post_id], :user_id => session[:user_id], :body => params[:body]) or words to that effect. (It''s very unlikely that you really should be passing a value for the id field; that''s generally handled automatically with an integer value.) It might be helpful to try out some comment creation in the application console and make sure you understand what the create method expects and what results you get. The console is a great learning and exploring tool. David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Stay tuned for next event announcement! Rubyist http://www.compleatrubyist.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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You have to show us the code in your view that has the form to make a comment. From your description, params[:body] is obviously not being set to anything because you don''t have a textarea or input box with the name "body". If the name of the textarea or input is named "comment[body]" then to access this value it would be params[:comment][:body]. Also if you have an association between users and comments, such as user has_many :comments, you can just do @blogcomment = @user.blogcomments.create(:id => params[:body]) -- 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.
Hello, Yeah below is the code from the form that creates the comment: <%= form_tag :action => "comment", :id => @blogpost %> <%= text_area "comment", "body", :cols => 40, :rows => 6 %><br /> <%= submit_tag "Comment" %> I tried - @blogcomment = @user.blogcomments.create(:id => params[:body]) but this still remains a null value... On 11 July, 23:03, Kenneth <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> You have to show us the code in your view that has the form to make a > comment. > > From your description, params[:body] is obviously not being set to > anything because you don''t have a textarea or input box with the name > "body". If the name of the textarea or input is named "comment[body]" > then to access this value it would be params[:comment][:body]. > > Also if you have an association between users and comments, such as user > has_many :comments, you can just do > > @blogcomment = @user.blogcomments.create(:id => params[:body]) > -- > Posted viahttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
RubyonRails_newbie wrote:> <%= form_tag :action => "comment", :id => @blogpost %> > <%= text_area "comment", "body", :cols => 40, :rows => 6 %><br /> > <%= submit_tag "Comment" %> > I tried - @blogcomment = @user.blogcomments.create(:id => > params[:body]) but this still remains a null value...With "text_area :comment, :body, ..." you should use this "params[:comment][:body]" instead. Which is what Kenneth said: Kenneth wrote:> From your description, params[:body] is obviously not being set to > anything because you don''t have a textarea or input box with the name > "body". If the name of the textarea or input is named "comment[body]" > then to access this value it would be params[:comment][:body].Also as someone in the thread mentioned you probably shouldn''t be setting :id because the database will assign a unique one for you. Instead you should be setting the relation id. Without seeing the code it is difficult but I would guess something like this: @blogcomment = @blogcomment.create(:user_id => @user.id, :body => params[:comment][:body]) Or as suggested already use the association. Which is probably the best way. Without seeing the code it is difficult but I would guess something like this: @blogcomment = @user.blogcomments.create(:body => params[:comment][:body]) Bob -- 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.
Awesome, thanks for the advice!!! On 12 July, 19:02, Bob Proulx <b...-5cAygf9QrE/QT0dZR+AlfA@public.gmane.org> wrote:> RubyonRails_newbie wrote: > > <%= form_tag :action => "comment", :id => @blogpost %> > > <%= text_area "comment", "body", :cols => 40, :rows => 6 %><br /> > > <%= submit_tag "Comment" %> > > I tried - @blogcomment = @user.blogcomments.create(:id => > > params[:body]) but this still remains a null value... > > With "text_area :comment, :body, ..." you should use this > "params[:comment][:body]" instead. Which is what Kenneth said: > > Kenneth wrote: > > From your description, params[:body] is obviously not being set to > > anything because you don''t have a textarea or input box with the name > > "body". If the name of the textarea or input is named "comment[body]" > > then to access this value it would be params[:comment][:body]. > > Also as someone in the thread mentioned you probably shouldn''t be > setting :id because the database will assign a unique one for you. > Instead you should be setting the relation id. Without seeing the > code it is difficult but I would guess something like this: > > @blogcomment = @blogcomment.create(:user_id => @user.id, > :body => params[:comment][:body]) > > Or as suggested already use the association. Which is probably the > best way. Without seeing the code it is difficult but I would guess > something like this: > > @blogcomment = @user.blogcomments.create(:body => params[:comment][:body]) > > Bob-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.