# Problem with params\[\]

**URL:** <https://www.ruby-forum.com/t/problem-with-params/159157>\
**Category:** Rails\
**Created:** [February 1, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/problem-with-params/159157 "2009-02-01T16:25:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [February 1, 2009, 4:25pm UTC](https://www.ruby-forum.com/t/problem-with-params/159157/1 "2009-02-01T16:25:47Z")

</div>

Hi!  
I’m building a discussion forum in my rails app and there seems to be  
a problem in creating a new topic in the forum.  
I’m using three models for it forum, topic and post.  
The topic model has following fields:  
t.integer :forum\_id  
t.integer :user\_id  
t.string :name  
t.datetime :created\_at  
t.datetime :updated\_at  
t.integer :posts\_count,:null =\> false, :default =\> 0

the problem comes when i try to create a new topic in a forum. The  
controller has following code:  
def create

```
@topic = Topic.new(:name => params[:topic][:name], :forum_id =>

```

params[:forum\_id], :user\_id =\> logged\_in\_user.id)

```
@topic.save!

@post = Post.new(:body => params[:post][:body],
                 :topic_id => @topic.id,
                 :user_id => logged_in_user.id)
@post.save!

respond_to do |format|
 format.html { redirect_to posts_path(:topic_id =>

```

@topic, :forum\_id =\> @topic.forum.id) }  
format.xml { head :created, :location =\> topic\_path(:id =\>  
@topic, :forum\_id =\> @topic.forum.id) }  
end  
rescue ActiveRecord::RecordInvalid  
respond\_to do |format|  
format.html { render :action =\> ‘new’ }  
format.xml { render :xml =\> @post.errors.to\_xml }  
end  
end

Now when the row is inserted into the topics table the forum\_id field  
is blank. I cant figure out what’s happening here. Please help…

---

<div class="post-metadata">

**Author:** ![rick\_2047](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecd19e/32.png) [@rick\_2047](https://www.ruby-forum.com/u/rick_2047)\
**Post date:** [February 1, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/problem-with-params/159157/2 "2009-02-01T18:25:40Z")

</div>

Hi Rick,

Do you have validations in you models?  
I mean like  
validates\_presence\_of :forum\_id

Besides that, I’d do a @topic.posts.new instead of Post.new

And third, please do a @forum = Forum.find(params[:forum\_id])

And fourth, maybe its params[:topic][:forum\_id] ??

ciao, tom
