带有嵌套资源的 form_for [英] form_for with nested resources

查看:33
本文介绍了带有嵌套资源的 form_for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a two-part question about form_for and nested resources. Let's say I'm writing a blog engine and I want to relate a comment to an article. I've defined a nested resource as follows:

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

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

The comment form is in the show.html.erb view for articles, underneath the article itself, for instance like this:

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

这给出了一个错误,调用 id 为 nil,这会错误地等等."我也试过

This gives an error, "Called id for nil, which would mistakenly etc." I've also tried

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

它呈现正确但将 f.text_area 与文章的文本"字段而不是评论的字段相关联,并在该文本区域中显示 article.text 属性的 html.所以我似乎也有这个错误.我想要的是一个表单,它的提交"将调用 CommentsController 上的创建操作,参数中有一个 article_id,例如对/articles/1/comments 的发布请求.

Which renders correctly but relates f.text_area to the article's 'text' field instead of the comment's, and presents the html for the article.text attribute in that text area. So I seem to have this wrong as well. What I want is a form whose 'submit' will call the create action on CommentsController, with an article_id in the params, for instance a post request to /articles/1/comments.

我的问题的第二部分是,开始创建评论实例的最佳方法是什么?我在 ArticlesController 的 show 操作中创建了一个 @comment,所以一个评论对象将在 form_for 助手的范围内.然后在 CommentsController 的创建操作中,我使用从 form_for 传入的参数创建新的 @comment.

The second part to my question is, what's the best way to create the comment instance to begin with? I'm creating a @comment in the show action of the ArticlesController, so a comment object will be in scope for the form_for helper. Then in the create action of the CommentsController, I create new @comment using the params passed in from the form_for.

谢谢!

推荐答案

Travis R 是正确的.(我希望我能给你点赞.)我自己才开始工作.使用这些路线:

Travis R is correct. (I wish I could upvote ya.) I just got this working myself. With these routes:

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,没有特殊的命名空间.

just as it says at http://guides.rubyonrails.org/routing.html#nested-resources, with no special namespaces.

但是部分和形式变得棘手.注意方括号:

But partials and forms become tricky. Note the square brackets:

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

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

Most important, if you want a URI, you may need something like this:

article_comment_path(@article, @comment)

或者:

[@article, @comment]

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

例如,在为迭代提供了 comment_item 的集合部分中,

For example, inside a collections partial with comment_item supplied for iteration,

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

jamuraa 所说的在文章的上下文中可能有效,但在其他各种方面对我都不起作用.

What jamuraa says may work in the context of Article, but it did not work for me in various other ways.

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

There is a lot of discussion related to nested resources, e.g. http://weblog.jamisbuck.org/2007/2/5/nesting-resources

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

Interestingly, I just learned that most people's unit-tests are not actually testing all paths. When people follow jamisbuck's suggestion, they end up with two ways to get at nested resources. Their unit-tests will generally get/post to the simplest:

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

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

In order to test the route that they may prefer, they need to do it this way:

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

我了解到这一点是因为我的单元测试在我从这里切换时开始失败:

I learned this because my unit-tests started failing when I switched from this:

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

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

I guess it's ok to have duplicate routes, and to miss a few unit-tests. (Why test? Because even if the user never sees the duplicates, your forms may refer to them, either implicitly or via named routes.) Still, to minimize needless duplication, I recommend this:

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

抱歉回答太长.我想,没有多少人意识到其中的微妙之处.

Sorry for the long answer. Not many people are aware of the subtleties, I think.

这篇关于带有嵌套资源的 form_for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆