如何在<%= f.select_tag%>中设置所选值下拉列表,怎么样?

时间:2013-09-26 07:26:51

标签: forms ruby-on-rails-4

我有一个包含所有货币的长列表下拉菜单,我希望最后选择的值是列表中的默认选定值。 我正在使用Rails 4.0.0和ruby 2.0.0

我没有使用模型,我只是使用控制器。我已将下拉列表选项放在帮助器中。

form.html.erb

<div class="calculator">

    <%= form_for :convertor, :url => {:controller => "converter", :action => "show" } do |f| %>

        <%= f.label :from_currency %>
        <%= f.select :from_currency, options_for_select(currencies, :selected => params[:from_currency]) %>
        <%= f.text_field :amount, :placeholder => "Amount", id: "textfield" %>
        <%= @amount %>
        <br>

        <%= f.label :to_currency %>
        <%= f.select :to_currency, options_for_select(currencies, :selected => params[:to_currency].to_i ) %>
        <%= @result %>
        <br>

        <%= f.submit "Calculate", class: "btn btn-large btn-primary", id: "submitButton" %> 
    <% end %>

</div>

列表选项从帮助器加载*&lt;%= f.select:to_currency,options_for_select(currency,:selected =&gt; params [:to_currency] .to_i)%&gt; * with name currency

下拉列表,在帮助程序

def currencies
    [
        ['United Arab Emirates Dirham (AED)', 'AED'],
        ['Netherlands Antillean Guilder (ANG)', 'ANG'],
        ['Argentine Peso (ARS)', 'ARS'],
        ['Australian Dollar (A$)', 'AUD'],
        ['Bangladeshi Taka (BDT)', 'BDT'],
        ['Bulgarian Lev (BGN)', 'BGN'],
        ['Bahraini Dinar (BHD)', 'BHD'],
    ]
end

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

有几件事:

  1. options_for_select takes two arguments:选项列表和您想要选择的值。
  2. 您将该值作为哈希值传递::selected => params...
  3. 参数名称错误。
  4. 在你的控制器中,你说这个参数叫做:

    params[:convertor][:from_currency]
    

    但在您看来,您有params[:from_currency]

    因此,解决方案是这样做:

    <%= f.select :from_currency, options_for_select(currencies, params[:convertor][:from_currency]) %>
    

答案 1 :(得分:0)

此代码适用于我。

在应用程序助手

module ApplicationHelper
  def opts_for_select_cur
    opts_for_select = [
      ['United Arab Emirates Dirham (AED)', 'AED'],
      ['Netherlands Antillean Guilder (ANG)', 'ANG'],
      ['Argentine Peso (ARS)', 'ARS'],
      ['Australian Dollar (A$)', 'AUD'],
      ['Bangladeshi Taka (BDT)', 'BDT'],
      ['Bulgarian Lev (BGN)', 'BGN'],
      ['Bahraini Dinar (BHD)', 'BHD'],
    ]

    return opts_for_select
  end
end

并在视野中

<script type="text/javascript">
 $(document).ready(function(){

 $('#st').change(function(){
  var inputText = $('#st :selected').val();
  $("#hidden_one").val(inputText);

  $("a").attr("href", "?value="+inputText );

 });
 $('#aa').click(function(){
  var inputText = $('#st :selected').val();
  $("a").attr("href", "?value="+inputText );
});

});

<%= f.select :text, options_for_select(opts_for_select_cur, params[:value]), {}, { id: "st" } %>
<br>
<%= link_to "refresh",nil, id: "aa" %>

结果是一个带有所选值的选择框。

答案 2 :(得分:0)

这可以帮助

<%= f.label :from_currency %>
            <% if @from_cur.present? %>
                <%= f.select :from_currency, options_for_select(currencies,  @from_cur)  %>
            <% else %>
                <%= f.select :from_currency, options_for_select(currencies), :required => true %>
            <% end %>
            <%= f.text_field :amount, :placeholder => "Amount", :required => true, id: "textfield"  %>
            <br>

            <%= f.label :to_currency %>
            <% if @from_cur.present? %>
                <%= f.select :to_currency, options_for_select(currencies,  @to_cur), :required => true %>
            <% else %>
                <%= f.select :to_currency, options_for_select(currencies), :required => true %>
            <% end %>

我很感激。