ruby on Rails 选择不默认为当前值
我目前正在使用如下选择(在表单内):
<% 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 %>
任何帮助使其工作的帮助将不胜感激!
I'm currently using a select as follows (within a form):
<% form_for :search, :url => search_path, :html => {:method => :get} do |f| %>
<%= select('search', :type, options_for_select(['Artist', 'Track'])) %>
<%= f.text_field :query %>
<% end %>
This works, but when I perform a search, it defaults back to artist even if the user selected Track before searching. How can I correct this?
Automatically selecting the current value works for radio buttons:
<% 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 %>
Any help getting this to work will be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用 select 表单生成器:
You must use the select form builder: