1

我有一个包含所有货币的长列表下拉菜单,我希望最后选择的值是列表中的默认选择值。我正在使用 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>

列表选项从帮助器 *<%= f.select :to_currency, options_for_select(currencies, :selected => params[:to_currency].to_i ) %>* 加载,名称为 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

我在这里做错了什么?

4

3 回答 3

1

有几件事:

  1. options_for_select接受两个参数:选项列表和您要选择的值。
  2. 您将值作为哈希传递::selected => params...
  3. 参数名称错误。

在您的控制器中,您说该参数被称为:

params[:convertor][:from_currency]

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

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

<%= f.select :from_currency, options_for_select(currencies, params[:convertor][:from_currency]) %>
于 2013-09-26T07:31:35.883 回答
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" %>

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

于 2013-09-26T08:15:56.220 回答
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 %>

我很感激。

于 2013-09-28T07:51:41.007 回答