【问题标题】:ruby on rails select not defaulting to current valueruby on rails 选择不默认为当前值
【发布时间】:2011-06-16 00:10:45
【问题描述】:

我目前正在使用如下选择(在表单中):

<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
        <%=  select('search', :type, options_for_select(['Artist', 'Track'])) %>
        <%= f.text_field :query %>
<% end %>

这可行,但是当我执行搜索时,即使用户在搜索之前选择了曲目,它也会默认返回艺术家。我该如何纠正这个问题?

自动选择当前值适用于单选按钮:

<% form_for @search, :url => search_path, :html => {:method => :get} do |f| %>
  <p class="radio_button">
  <%= f.label :type_track, 'Search tracks' %>
  <%= f.radio_button :type, 'Track' %>
  </p>
  <p class="radio_button">
  <%= f.label :type_artist, 'Search artists' %>
  <%= f.radio_button :type, 'Artist' %>
  </p>
  <p class="text_field">
  <%= f.label :query, 'Search query' %>
  <%= f.text_field :query, :class => 'auto_focus' %>
  </p>
  <p class="submit">
  <%= submit_tag 'Go' %>
  </p>
<% end %>

我们将不胜感激任何帮助使其发挥作用!

【问题讨论】:

    标签: ruby-on-rails forms select


    【解决方案1】:

    您必须使用select 表单生成器:

    <%= f.select(:type, [["text1", "value1"], ["text2", "value2"], ...]) %>
    

    【讨论】:

    • 抱歉——pairs_name_id 数组应该是什么样子的......?
    • [["text", value], ["text2", "value2"]]
    • 你应该接受tokland的答案,当你收到正确答案时这是最少的
    • 我要去!不过,我不得不等待足够长的时间来标记它,然后不得不离开我的办公室。
    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 2017-04-08
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多