具有嵌套资源的form_for

问题描述 投票:112回答:3

我有一个关于form_for和嵌套资源的两部分问题。假设我正在编写一个博客引擎,我想将评论与文章联系起来。我已经定义了一个嵌套资源,如下所示:

map.resources :articles do |articles|
    articles.resources :comments
end

评论表单位于文章的show.html.erb视图中,位于文章本身下方,例如:

<%= render :partial => "articles/article" %>
<% form_for([ :article, @comment]) do |f| %>
    <%= f.text_area :text %>
    <%= submit_tag "Submit" %>
<%  end %>

这给出了一个错误,“为nil调用id,这会错误等等”。我也试过了

<% form_for @article, @comment do |f| %>

哪个呈现正确但将f.text_area与文章的“文本”字段而不是注释相关联,并在该文本区域中显示了article.text属性的html。所以我似乎也有这个错误。我想要的是一个表单,其'submit'将在CommentsController上调用create action,在params中有一个article_id,例如对/ articles / 1 / comments的post请求。

我的问题的第二部分是,开始创建评论实例的最佳方法是什么?我正在ArticlesController的show动作中创建一个@comment,因此一个注释对象将在form_for帮助器的范围内。然后在CommentsController的create动作中,我使用从form_for传入的参数创建新的@comment。

谢谢!

ruby-on-rails form-for nested-resources
3个回答
214
投票

特拉维斯R是对的。 (我希望我可以赞成你。)我自己就这样做了。有了这些路线:

resources :articles do
  resources :comments
end

你得到的路径如下:

/articles/42
/articles/42/comments/99

路由到控制器

app/controllers/articles_controller.rb
app/controllers/comments_controller.rb

正如它在http://guides.rubyonrails.org/routing.html#nested-resources所说,没有特殊的命名空间。

但局部和形式变得棘手。注意方括号:

<%= form_for [@article, @comment] do |f| %>

最重要的是,如果你想要一个URI,你可能需要这样的东西:

article_comment_path(@article, @comment)

或者:

[@article, @comment]

http://edgeguides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects所述

例如,在一个集合内部,为comment_item提供迭代,

<%= link_to "delete", article_comment_path(@article, comment_item),
      :method => :delete, :confirm => "Really?" %>

jamuraa所说的可能在文章的背景下起作用,但它在各种其他方面对我不起作用。

有很多关于嵌套资源的讨论,例如http://weblog.jamisbuck.org/2007/2/5/nesting-resources

有趣的是,我刚刚了解到大多数人的单元测试实际上并没有测试所有路径。当人们遵循jamisbuck的建议时,他们最终会有两种方法来获取嵌套资源。他们的单元测试通常会得到/发布到最简单的:

# POST /comments
post :create, :comment => {:article_id=>42, ...}

为了测试他们可能更喜欢的路线,他们需要这样做:

# POST /articles/42/comments
post :create, :article_id => 42, :comment => {...}

我学会了这个,因为当我从这里切换时,我的单元测试开始失败:

resources :comments
resources :articles do
  resources :comments
end

对此:

resources :comments, :only => [:destroy, :show, :edit, :update]
resources :articles do
  resources :comments, :only => [:create, :index, :new]
end

我想有重复的路线,并错过一些单元测试是可以的。 (为什么测试?因为即使用户从未看到重复项,您的表单也可以隐式或通过命名路由引用它们。)尽管如此,为了最大限度地减少不必要的重复,我建议:

resources :comments
resources :articles do
  resources :comments, :only => [:create, :index, :new]
end

对不起,答案很长。我想,没有多少人知道这些微妙之处。


52
投票

一定要在控制器中创建两个对象:@post@comment作为帖子,例如:

@post = Post.find params[:post_id]
@comment = Comment.new(:post=>@post)

然后在视图中:

<%= form_for([@post, @comment]) do |f| %>

一定要在form_for中明确定义数组,而不仅仅是像上面那样用逗号分隔。


33
投票

您不需要在表单中执行特殊操作。您只需在show动作中正确构建注释:

class ArticlesController < ActionController::Base
  ....
  def show
    @article = Article.find(params[:id])
    @new_comment = @article.comments.build
  end
  ....
end

然后在文章视图中为它创建一个表单:

<% form_for @new_comment do |f| %>
   <%= f.text_area :text %>
   <%= f.submit "Post Comment" %>
<% end %>

默认情况下,这个评论将转到createCommentsController动作,然后你可能会想把redirect :back放进去,这样你就可以回到Article页面了。

© www.soinside.com 2019 - 2024. All rights reserved.