# Null values

**URL:** <https://www.ruby-forum.com/t/null-values/191396>\
**Category:** Rails\
**Created:** [July 11, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/null-values/191396 "2010-07-11T21:59:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 11, 2010, 9:59pm UTC](https://www.ruby-forum.com/t/null-values/191396/1 "2010-07-11T21:59:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 11, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/null-values/191396/2 "2010-07-11T22:24:48Z")

</div>

On Sun, 11 Jul 2010, RubyonRails\_newbie wrote:

> ```
> @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](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 11, 2010, 10:38pm UTC](https://www.ruby-forum.com/t/null-values/191396/3 "2010-07-11T22:38:51Z")

</div>

I tried both :body and :id but the same issue:

@blogcomment = Blogcomment.create(:id =\> params[:body], :user\_id =\>  
session[:user\_id])

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 11, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/null-values/191396/4 "2010-07-11T23:26:17Z")

</div>

On Sun, 11 Jul 2010, RubyonRails\_newbie wrote:

> > 
> 
> @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](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 12, 2010, 12:03am UTC](https://www.ruby-forum.com/t/null-values/191396/5 "2010-07-12T00:03:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 12, 2010, 6:47pm UTC](https://www.ruby-forum.com/t/null-values/191396/6 "2010-07-12T18:47:37Z")

</div>

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 %\>  
  
\<%= submit\_tag “Comment” %\>

I tried - @blogcomment = @user.blogcomments.create(:id =\>  
params[:body]) but this still remains a null value…

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 12, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/null-values/191396/7 "2010-07-12T22:11:24Z")

</div>

Awesome,

thanks for the advice!!!

---

<div class="post-metadata">

**Author:** ![RubyonRails\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@RubyonRails\_newbie](https://www.ruby-forum.com/u/RubyonRails_newbie)\
**Post date:** [July 12, 2010, 8:03pm UTC](https://www.ruby-forum.com/t/null-values/191396/8 "2010-07-12T20:03:46Z")

</div>

RubyonRails\_newbie wrote:

> \<%= form\_tag :action =\> “comment”, :id =\> @blogpost %\>  
> \<%= text\_area “comment”, “body”, :cols =\> 40, :rows =\> 6 %\>  
>   
> \<%= 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
