socm_
socm_

Reputation: 787

params[:comment] where author give :comment?

I'm want to understand where author give :comment, which come to controller from this form

    <h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <p>
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
  </p>
  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

And class and action is

  class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
  end
end

where author give :comment? how he can receive that without claiming in the form?

Upvotes: 0

Views: 68

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84142

Because the form_for is operating on an object of class Comment, the generated names for the fields namespace the params in params[:comment].

You could change this by passing the :as option to form_for, but this isn't normally needed.

Upvotes: 1

Related Questions