form_for 与关联 - 如何提供父 ID?

发布于 2024-10-02 11:06:39 字数 583 浏览 6 评论 0原文

假设带有嵌套资源的 Post - Comment 模型:

resources :posts do
  resources :comments
end

app/views/comments/_form.html.haml 应该如何(erb 会做以及)看起来像这样它还提供了要附加评论的帖子的ID?

目前我知道的唯一一种方法是手动添加带有帖子 ID 的隐藏输入。在我看来它很脏。

还有更好的办法吗?我希望 Rails 能够理解嵌套资源并自动包含 post_id 作为隐藏输入。

= form_for [@post, @comment] do |f|
  .field
    f.label :body
    f.text_field :body
    hidden_field_tag :post_id, @post.id
  .actions
    = f.submit 'Save'

编辑:使用 Mongoid,而不是 ActiveRecord。

谢谢。

Assuming the Post - Comment model with the nested resources:

resources :posts do
  resources :comments
end

How should the app/views/comments/_form.html.haml (erb will do as well) look like so that it also provides id of the post to attach the comment to?

Current only one way I know is to manually add hidden input with the post id. It looks dirty to me.

Is there any better way? I expected rails to understand the nested resource and automatically include the post_id as a hidden input.

= form_for [@post, @comment] do |f|
  .field
    f.label :body
    f.text_field :body
    hidden_field_tag :post_id, @post.id
  .actions
    = f.submit 'Save'

EDIT: Using Mongoid, not ActiveRecord.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-10-09 11:06:39

帖子的 ID 实际上位于 URL 中。如果您在终端/控制台中输入 rake paths ,您将看到嵌套资源的模式定义如下:

POST    /posts/:post_id/comments    {:controller=>"comments", :action=>"create"}

看一下 form_for< 吐出的 HTML /code> 方法,并具体查看

标记的 action url。您应该看到类似于 action="/posts/4/comments" 的内容。

假设您在 routes.rb 中仅定义 一次 resources :comments,作为 resources :posts< 的嵌套资源/code>,那么您可以安全地修改 CommentsController#create 操作:

# retrieve the post for this comment
post = Post.find(params[:post_id])
comment = Comment.new(params[:comment])
comment.post = post

或者您可以简单地将 params[:post_id] 传递给 comment< /code> 实例如下:

comment.post_id = params[:post_id]

我希望这有帮助。

有关嵌套表单/模型的更多信息,我建议观看以下 Railscast:

The ID of the post will actually be in the URL. If you type rake routes into your terminal/console, you will see that the pattern for your nested resource is defined as such:

POST    /posts/:post_id/comments    {:controller=>"comments", :action=>"create"}

Take a look at the HTML that is spit out by the form_for method, and look specifically at the action url of the <form> tag. You should see something like action="/posts/4/comments".

Assuming that you've defined resources :comments only once in your routes.rb, as a nested resource of resources :posts, then it is safe for you to modify the CommentsController#create action as such:

# retrieve the post for this comment
post = Post.find(params[:post_id])
comment = Comment.new(params[:comment])
comment.post = post

Or you can simply pass params[:post_id] to comment instance like so:

comment.post_id = params[:post_id]

I hope this helps.

For more information on nested forms/models, I recommend watching the following Railscasts:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文