Topic: Form_for cant get topic_id and user_id through posts_controller

Hello
I am preparing an application which User can create a Topic and others can send a Post as comments. In my homepage i use the Topics as a list and Topic form as partials without any problems. When you click a Topic , I want to show the Topic as a heading and posts as a list and a post_form which is another partial.

I nested my resources like :
   

resources :topics , only: [:show, :create, :destroy] do
  resources :posts, only: [:create, :show, :new]
       

this is my posts_controller.rb :

class PostsController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user, only: :destroy

  def new
   @topic= Topic.find_by_id(params[:id])
   @post = @topic.posts.build(params[:post])
  end

  def show
    @post= Post.find(params[:id])
    @topic= @post.topic
  end 

  def create
    @post = @topic.posts.build(params[:post])
    @post.topic = Topic.find(params[:topic_id])
    @post.user = current_user
    if @post.save
       flash[:success] = "Konu oluşturuldu!"
       redirect_to topic_path(topic)
    else
  render 'static_pages/home'
    end
end  
  def destroy
    @post.destroy
    redirect_to root_path
  end
  private
    def correct_user
      @post = current_user.posts.find_by_id(params[:id])
      redirect_to root_path if @post.nil?
    end
end
and my topics_controller show action :
  def show    
  @topic = Topic.find(params[:id])
  @posts = @topic.posts.paginate(page: params[:page])
  @post = @topic.posts.build if signed_in?
  end

My _post_form_html.erb :

<%= form_for [@topic, @post] do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "yorumunuzu girin..." %>
  </div>
  <%= f.submit "Gönder", class: "btn btn-large btn-primary" %>
<% end %>

I can't make posts.form top accept topic_id and user_id from controller ? Can you help me?

Last edited by ytsejam (2012-06-27 07:33:08)

Re: Form_for cant get topic_id and user_id through posts_controller

Stupid question, perhaps, but how are your models configured?  I believe they need to reflect the one to many relationship.  This will ensure that the topic_id is populated correctly, I would think.