为什么form_for方法将符号作为参数?

时间:2017-07-02 13:24:11

标签: ruby-on-rails ruby

我正在研究Rails博客教程,语法如下:

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

我真的不明白辅助方法或方法何时将符号作为参数,何时不是...

2 个答案:

答案 0 :(得分:3)

form_for接受多种类型的参数。

(a)symbol:form_for:article

mysqli_query($connessione,"INSERT INTO earthquakes  (id,idserial,milliseconds,latitude,longitude,magnitude,ipocentro,source,region) VALUES $values");

(b)string:form_for&#34; article&#34;

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

(c)object:form_for @article

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

(d)array:form_for [:my,@ 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

答案 1 :(得分:0)

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

回答&#34;为什么&#34;的问题:默认使用符号,因为比较比直接字符串比较快。