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
On Feb 1, 4:25 pm, rick_2047
<rick.chatter...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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....
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@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
-~----------~----~----~----~------~----~------~--~---