title: Insert data into multiple tables from one controller/view [Rails 4]
body: <p>I have more curious questions for all you amazing people!</p>
  
  <p>I am creating a forum and when you create a topic, you are also creating the first post at the same time. </p>
  
  <p>I need to assign variables to certain fields. </p>
  
  <p>Example: :user_id => current_user.id,</p>
  
  <p>I don't have the param settings correct, so many of the fields are NULL when stored in the database. </p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>class Topic &lt; ActiveRecord::Base
    belongs_to :forum
    has_many :posts, :dependent =&gt; :destroy  
    belongs_to :user
    accepts_nested_attributes_for :posts
  end
  
  class Post &lt; ActiveRecord::Base  
    belongs_to :topic  
    belongs_to :user
  end
  </code></pre>
  
  <p><strong>Topics Controller</strong></p>
  
  <pre><code># GET /topics/new
  def new
    @topic = Topic.new
    @topic.posts.build
  end
  
  def create  
    @topic = Topic.new(topic_params)
  
    if @topic.save 
      #@topic.responses = Post.new(params[:responses])
      flash[:success] = "Topic Posted"
      redirect_to "/forums/#{@topic.forum_id}" 
    else  
      render :new  
    end   
  end
  
  def topic_params
    # last_post_at = (:last_post_at =&gt; Time.now)
    params.require(:topic).permit(
      :name, 
      :description, 
      [:last_poster_id =&gt; current_user.id], 
      [:last_post_at =&gt; Time.now], 
      [:user_id =&gt; current_user.id],
      :forum_id,
      posts_attributes: [:id, :content, :topic_id, :user_id =&gt; current.user.id] )
  end
  </code></pre>
  
  <p><strong>Post Controller</strong></p>
  
  <pre><code># GET /posts/new
  def new
    @post = Post.new
  end
  
  def create  
    @post = Post.new(
      :content =&gt; params[:post][:content], 
      :topic_id =&gt; params[:post][:topic_id], 
      :user_id =&gt; current_user.id)  
  
    if @post.save  
      @topic = Topic.find(@post.topic_id)  
      @topic.update_attributes(
        :last_poster_id =&gt; current_user.id, 
        :last_post_at =&gt; Time.now)  
      flash[:notice] = "Successfully created post."  
      redirect_to "/topics/#{@post.topic_id}"  
    else  
      render :action =&gt; 'new'  
    end  
  end  
  </code></pre>
  
  <p><strong>_form for View/Topic</strong></p>
  
  <pre><code>&lt;%= form_for(@topic) do |f| %&gt;
    &lt;% if params[:forum] %&gt;
      &lt;input type="hidden" 
      id="topic_forum_id" 
      name="topic[forum_id]" 
      value="&lt;%= params[:forum] %&gt;" /&gt;
    &lt;% end %&gt;  
  
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :description %&gt;&lt;br&gt;
      &lt;%= f.text_field :description %&gt;
    &lt;/div&gt;
  
    &lt;%= f.fields_for :posts do |p| %&gt;
  
      &lt;%= p.label :content %&gt;&lt;br /&gt;
      &lt;%= p.text_area :content %&gt;
  
    &lt;% end %&gt;
    &lt;%= f.submit :class =&gt; "btn btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>