null values...

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

Do you mean :body there, rather than :id?

David

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

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])

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...

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

Awesome,

thanks for the advice!!!