Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails haml中呈现文本字段_Ruby On Rails_Haml - Fatal编程技术网

Ruby on rails 如何在rails haml中呈现文本字段

Ruby on rails 如何在rails haml中呈现文本字段,ruby-on-rails,haml,Ruby On Rails,Haml,我想显示带有特定参数的文本字段。当我使用: %input{:type=>"text", :name => "search_name",:maxlenght => "200" ,:class => "text_field", :placeholder => t("homepage.save_search.save_field_placeholder") } 一切正常,生成的html就是我想要的: <input class="text_field" maxlen

我想显示带有特定参数的文本字段。当我使用:

%input{:type=>"text", :name => "search_name",:maxlenght => "200" ,:class => "text_field", :placeholder => t("homepage.save_search.save_field_placeholder") }
一切正常,生成的html就是我想要的:

<input class="text_field" maxlenght="200" name="search_name" placeholder="Search name" type="text">
它生成以下html:

<input type="text" name="search_name[{:maxlenght=>&quot;200&quot;, :class=>&quot;text_field&quot;, :placeholder=>&quot;Search name&quot;}]" id="search_name_{:maxlenght=>&quot;200&quot;, :class=>&quot;text_field&quot;, :placeholder=>&quot;Search name&quot;}">


阅读rails和haml文件生成文本字段的其他示例,我认为这两种解决方案是等效的。任何人都可以解释这一区别?

在前一行中,您正在创建一个文本字段,后一种语法是处理对象时使用的辅助工具。请看一下方法签名

text_field(object_name, method, options = {}) public
对于这些帮助程序,第一个参数是实例变量的名称,第二个参数是调用该对象的方法(通常是属性)的名称。Rails将输入控件的值设置为对象的该方法的返回值,并设置适当的输入名称。如果您的控制器已定义@person,且此人的姓名为Henry,则会生成一个包含以下内容的表单:


将产生类似于

<input id="person_name" name="person[name]" type="text" value="Henry"/>


尝试将
=text\u field..
替换为
=text\u field\u tag…
<%= text_field(:person, :name) %>
<input id="person_name" name="person[name]" type="text" value="Henry"/>