4-6 select_tag和select的区别和理解。javascript_tag

via: :all是什么意思?主要用于约束http动作。


<%= select_tag "set_locale", options_for_select(LANGUAGES, I18n.locale.to_s),
          onchange: "this.form.submit()"%>

⚠️ ,和select不一样,不能简写。 

解析:

<select name="set_locale" id="set_locale" onchange="this.form.submit()">

  <option selected="selected" value="en">English</option>

  <option value="es">ESpañol</option>

</select>


理解:

LANGUAGES => [["English", "en"], ["ESpa&ntilde;ol", "es"]] 这是写在initializers/i18n.rb中的常量。
I18n.locale => :en,翻译成html,是用于设置当前option的selected位置/焦点。

api文档实例

select_tag "credit_card", options_for_select([ "VISA", "MasterCard" ], "MasterCard")
# => <select id="credit_card" name="credit_card">
     	<option>VISA</option>
#    	<option selected="selected">MasterCard</option>
     </select> 

select和select_tag的区别,类就不一样

select方法属于ActionView::Helpers::FormBuilder

FormBuilder object is associated with a particular model object and allows you to generate fields associated with the model object. 
<%= form_with(model: product, local: true) do |form| %>

   <%= form.select ( :locale, options_for_select(LANGUAGE, I18n.locale.to_s))%>

   ...

   (可以简写 <%= form.select( :locale, LANGUAGES)%>)

    配合chrome的inspect功能,可以看到效果,和错误

解析为html:

<select name="product[locale]">

  <option selected="selected" value="en">English</option>

  <option value="es">ESpañol</option>

</select>


 <%= javascript_tag "$('.locale input').hide()"%>

Returns a JavaScript tag with the content inside. Example:

javascript_tag "alert('All is good')" 

Returns:

<script> //<![CDATA[ alert('All is good') //]]> </script>
原文地址:https://www.cnblogs.com/chentianwei/p/8726903.html