我想知道如何获得<select>
的选定值。我只知道如何填充这个
这是我的代码在 index.html.erb
。我用它来填充<select>
下拉菜单。
<h1>Trap</h1>
<%= form_for @search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, @employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link @search, :empcode %> |
<%= sort_link @search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
控制器 traps_controller.rb
:
def index
@search = Trap.search(params[:search])
@traps = @search.all
@employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @traps }
end
end
请告诉我如何获得所选的值,如果用户选择一个值。我不知道。
Try(在控制器中):
value = params[:empcode_contains]
或者你可以这样获取整个对象:
search = params[:search]