2017-07-02 56 views
0

我工作的Rails的博客教程和语法云:为什么form_for方法以符号为参数?

<%= form_for :article do |f| %> 

我真的不明白,当一个辅助方法或方法接受一个符号作为参数,当它不...

+0

https://apidock.com/rails/ActionView/Helpers/FormHelper/form_for – Pavan

+0

您可以在#new视图中使用':article':以前没有记录:文章存在,您正在创建一篇新文章,以便您只需要告诉栏杆这是你正在做的一篇文章的一种形式。在编辑视图中,您可以使用'@ article'而不是':article',然后表单将为您要修改的文章选取已知值(然后预填表格) – Maxence

回答

3

form_for接受多种类型的参数。

(a)中的符号:的form_for:文章

form elements can accessed in controller by params[:article] 

(b)中的字符串:的form_for “制品”

form elements can be accessed in controller by params[:article] 

(C)对象:的form_for @article

(1) Here @article is expected to contain an instance of Article. In the controller this form elements can be accessed through params[:article]. 

    (2) If a form is expected to be accessed through a custom name, it can be done by using :as operator. form_for @post, as: :my_post 

(d)array:form_for [:my,@article]

:my is used to namespace @article. With params[:my_article] form elements can be accessed in controller 
0

需要一些额外的细节来理解答案:该函数实际上将符号,字符串或路径作为可能的参数。

回答“为什么”的问题:缺省使用符号,因为比较比直接字符串比较快。

+0

我不确定这是是一个实际的*“答案”*。似乎更适合作为评论 – engineersmnky

相关问题