关于Rails View中的tag:select相关

关于Rails View中的tag:select相关

select_tag主要是用于简单的生成select下拉框,如
select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>"

可以将它和options_for_select配合使用。

options_for_select主要是用来生成select下拉框中的option组,如
options_for_select([["Dollar", "$"], ["Kroner", "DKK"]]) #=> <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>

<%=select_tag "target_bank", options_for_select(Bank.all.collect{|bank|[bank.bank_name,bank.bank_url]}) %>

当然也可以使用hash,如
options_for_select({ "Basic" => "$20", "Plus" => "$40" }, "$40")

另外:
class Admin < ActiveRecord::Base
#admins表中有一个属性dept(integer)
DEPT = {"客服部" => 0, "业务部" => 1, "市场部" => 2}

  def department
    DEPT.invert.fetch(dept)
  end

end

在视图层这样写:
new.html.erb
  <p>
    <%= f.label :dept %><br />
    <%#= f.select :dept, {"请选择一个部门" => 0}.merge(Admin::DEPT), 0 %>
    <%=f.select :dept, Admin::DEPT, { :prompt => '请选择一个部门' }%>
  </p>


   <p>
    <%= f.label :article_category_id, "资讯分类" %><br />
    <%= f.select :article_category_id, ArticleCategory.all.collect{|category| [category.name, category.id]}, {:include_blank => '请选择资讯分类'} %>
  </p>




<%= select_tag 'show', options_for_select({"显示" => 1, "不显示" => 2}, 1) %>

这代码 怎么越看越漂亮了呢?

你可能感兴趣的:(F#,Ruby,资讯,Rails,ActiveRecord)