理解参数时遇到问题
问题描述:
我不明白在哪里:post_id和:评论来自“创建”操作。 from_for函数中没有提及它们。 http://guides.rubyonrails.org/getting_started.html“7.4生成器”:理解参数时遇到问题
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
end
答
form_for([@post, @post.comments.build])
会产生一种形式URL的作用“创建一个张贴评论”,这个网址有post_id
。而:comment
来自表单元素,其名称属性如name="comment[commenter]"
(来自:<%= f.text_field :commenter %>
),name="comment[body]"
(来自:<%= f.text_area :body %>
)等等。