form_for() 的多个参数 [英] Multiple parameters for form_for()

查看:34
本文介绍了form_for() 的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Beginning Rails 3.它创建了一个博客,用户可以发表文章,也可以对这些文章发表评论.它们看起来像这样:

I'm reading Beginning Rails 3. It creates a blog with Users who can post Articles and also post Comments to these Articles. They look like this:

    class User < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation
      attr_accessor :password

      has_many :articles, :order => 'published_at DESC, title ASC',
                          :dependent => :nullify
      has_many :replies, :through => :articles, :source => :comments

    class Article < ActiveRecord::Base
      attr_accessible :body, :excerpt, :location, :published_at, :title, :category_ids

      belongs_to :user
      has_many :comments

    class Comment < ActiveRecord::Base
      attr_accessible :article_id, :body, :email, :name
      belongs_to :article

在 app/views/comments/new.html.erb 中有一个这样开头的表单:

in app/views/comments/new.html.erb there's a form which begins like this:

    <%= form_for([@article, @article.comments.new]) do |f| %>

我的困惑在于为什么 form_for() 有两个参数.他们解决什么问题,为什么需要他们?

My confusion lies in why form_for() has two parameters. What do they resolve to and why are they necessary?

谢谢,迈克

推荐答案

实际上,在您的示例中,您使用 one 参数(即 Array)调用 form_for.如果您查看文档,您将看到它期望的参数:form_for(record, options = {}, &proc).在这种情况下,record 可以是 ActiveRecord 对象或数组(也可以是 String、Symbol 或类似 ActiveRecord 的对象).什么时候需要向它传递一个数组?

Actually, in your example, you are calling form_forwith one parameter (which is Array). If you check the documentation you will see parameters it expects: form_for(record, options = {}, &proc). In this case a record can be ActiveRecord object, or an Array (it can be also String, Symbol, or object that quacks like ActiveRecord). And when do you need to pass it an Array?

最简单的答案是,当您拥有嵌套资源时.就像在你的例子中一样,你已经定义了 Article has many Comments 关联.当您调用 rake routes 并正确定义了路由时,您会看到 Rails 为您的嵌套资源定义了不同的路由,例如:article_comments POST/article/:id/comments.

The simplest answer is, when you have a nested resource. Like in your example, you have defined Article has many Comments association. When you call rake routes, and have correctly defined routes, you will see that Rails has defined for you different routes for your nested resource, like: article_comments POST /article/:id/comments.

这很重要,因为您必须为表单标记创建有效的 URI(当然不是您,Rails 会为您完成).例如:

This is important, because you have to create valid URI for your form tag (well not you, Rails does it for you). For example:

form_for([@article, @comments])

你对 Rails 说的是:嘿,Rails,我给你一个对象数组作为第一个参数,因为你需要知道这个嵌套资源的 URI.我想以这种形式创建新的评论,所以我会给你一个 @comment = Comment.new 的初始实例.请为这篇文章创建这个评论:@article = Article.find(:id)."

What you are saying to Rails is: "Hey Rails, I am giving you Array of objects as a first parameter, because you need to know the URI for this nested resource. I want to create new comment in this form, so I will give you just initial instance of @comment = Comment.new. And please create this comment for this very article: @article = Article.find(:id)."

这大致类似于写作:

form_for(@comments, {:url => article_comments_path(@aticle.id)})

当然,这个故事还有更多内容,但应该足以掌握这个想法.

Of course, there is more to the story, but it should be enough, to grasp the idea.

这篇关于form_for() 的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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