title: Ruby on Rails / How to automatically add foreign key to my "comment" model?
body: <p>I generate 3 models: "User", "Article" and "Comment", and the "Comment" model have foreign key "user_id" and "article_id". However I can't automatically add "article_id" when I want to create an comment to a specific article in view. </p>
  
  <p>In rails console, I can add it successfully by using </p>
  
  <pre><code>comment = Comment.new(:content =&gt; "Great post")
  comment.user = user
  comment.article = Article.find(1)
  comment.save
  </code></pre>
  
  <p>I tried to write some code in my controller, but is doesn't work</p>
  
  <p>articles_controller.rb</p>
  
  <pre><code>class ArticlesController &lt; ApplicationController
    before_filter :signed_in_user, only: [:create, :destroy]
    before_filter :correct_user, only: :destroy 
  
    def index
      @articles = Article.all
    end
  
    def show
      @article = Article.find(params[:id])
      @comment = Comment.new()
    end
  
    def create
      @article = current_user.articles.build(params[:article])
      if @article.save
        flash[:success] = "Article created!"
        redirect_to root_url
      else
        render 'static_pages/home'
      end
    end
  
    def destroy
      @article.destroy
      redirect_to current_user
    end
  
    private
      def correct_user
        @article = current_user.articles.find_by_id(params[:id])
        redirect_to root_url if @article.nil?
      end
  end
  </code></pre>
  
  <p>comments_controller.rb</p>
  
  <pre><code>class CommentsController &lt; ApplicationController
    before_filter :signed_in_user, only: [:create, :destroy]
    before_filter :correct_user, only: :destroy 
  
    def create
      @comment = current_user.comments.build(params[:comment])
      if @comment.save
        flash[:success] = "Comment created!"
        redirect_to articles_url
      else
        render 'static_pages/home'
      end
    end
  
    def destroy
      @comment.destroy
      redirect_to current_user
    end
  
    private
      def correct_user
        @comment = current_user.comments.find_by_id(params[:id])
        redirect_to root_url if @comment.nil?
      end
  end
  </code></pre>
  
  <p>in view files
  /articles/show.html file render a partial file "_comment_form.html.erb"</p>
  
  <p>_comment_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@comment) do |f| %&gt;
    &lt;%= render 'shared/error_messages', object: f.object %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content, placeholder: "Compose your comment..." %&gt;
    &lt;/div&gt;
    &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I can add some new lines to manually add article_id to the comments table. But it is not a good way.</p>
  
  <pre><code>&lt;% @comment.article_id = @article.id %&gt;
  &lt;%= form_for(@comment) do |f| %&gt;
    &lt;%= render 'shared/error_messages', object: f.object %&gt;
    &lt;%= f.label :article_id %&gt;
    &lt;%= f.text_field :article_id %&gt;
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content, placeholder: "Compose your comment..." %&gt;
    &lt;/div&gt;
    &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>How can I do?
  Thank you so much.</p>
  
  <hr>
  
  <p>changed _comment_form.html.erb</p>
  
  <pre><code># views/comments/_comment_form.html.erb
  
  &lt;%= form_for[:articles, @comment] do |f| %&gt;
     &lt;%= render 'shared/error_messages', object: f.object %&gt;
     &lt;div class="field"&gt;
       &lt;%= f.text_area :content, placeholder: "Compose your comment..." %&gt;
     &lt;/div&gt;
     &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>SyntaxError in Articles#show
  
  Showing F:/RailsProject/project/gamespace/app/views/comments/_comment_form.html.erb where line #2 raised:
  
  F:/RailsProject/project/gamespace/app/views/comments/_comment_form.html.erb:2: syntax error, unexpected keyword_do_block, expecting keyword_end
  ...orm_for[:articles, @comment] do |f| @output_buffer.safe_conc...
  ...                               ^
  F:/RailsProject/project/gamespace/app/views/comments/_comment_form.html.erb:9: syntax error, unexpected keyword_ensure, expecting $end
  Extracted source (around line #2):
  
  1: 
  2:     &lt;%= form_for[:articles, @comment] do |f| %&gt;
  3:        &lt;%= render 'shared/error_messages', object: f.object %&gt;
  4:        &lt;div class="field"&gt;
  5:           &lt;%= f.text_area :content, placeholder: "Compose your comment..." %&gt;
  </code></pre>
  
  <hr>
  
  <pre><code> # comments_controller.rb
   class CommentsController &lt; ApplicationController
    before_filter :signed_in_user, only: [:create, :destroy]
    before_filter :correct_user, only: :destroy 
  
    def create
      @comment = Comment.new(comment_params)
      @comment.article_id = params[:id]
      if @comment.save
        flash[:success] = "Comment created!"
        redirect_to articles_url
      else
        render 'static_pages/home'
      end
    end
  
    def destroy
      @comment.destroy
      redirect_to current_user
    end
  
    private
  
      def correct_user
        @comment = current_user.comments.find_by_id(params[:id])
        redirect_to root_url if @comment.nil?
      end
  
      def comment_params
        params.require(:comment).permit(:content).merge(:user_id =&gt; current_user.id, :article_id =&gt; params[:id])
      end 
  end
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>TypeError in CommentsController#create
  
  can't convert Symbol into String
  Rails.root: F:/RailsProject/project/gamespace
  
  Application Trace | Framework Trace | Full Trace
  app/controllers/comments_controller.rb:33:in `comment_params'
  app/controllers/comments_controller.rb:7:in `create'
  This error occurred while loading the following files:
     comment
  Request
  
  Parameters:
  
  {"utf8"=&gt;"✓",
   "authenticity_token"=&gt;"ybkPs+c2068AZIIqCNJz1epBtS4L1zgT/vU9LL2Fs+E=",
   "comment"=&gt;{"content"=&gt;"sdfadfa"},
   "commit"=&gt;"Post",
   "article_id"=&gt;"10"}
  </code></pre>
  
  <h2>-------------------------------------------------------------------------------</h2>
  
  <hr>
  
  <p>Solution:</p>
  
  <pre><code># comments_controller.rb
  def create
    comment = current_user.comments.build(params[:comment])
    comment.article = Article.find(params[:article_id])
    comment.save
  end
  </code></pre>
  
  <p>use hidden_field method</p>
  
  <pre><code># views/_comments_form.html.erb
  &lt;%= form_for [@article, @comment] do |f| %&gt;  
     &lt;%= render 'shared/error_messages', object: f.object %&gt;
     &lt;%= f.hidden_field :article_id, :value =&gt; @article.id %&gt;
     &lt;div class="field"&gt;
       &lt;%= f.text_area :content, placeholder: "Compose your comment..." %&gt;
     &lt;/div&gt;
     &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>