从选择框将参数传递给控制器

时间:2012-01-11 05:18:17

标签: ruby-on-rails forms select parameter-passing

以下是我的观点:

 <%= form_tag({ :action => "display"}, :method => "get") do %>
   <%= select(:music, :type, MusType::TYPES,  {:include_blank => true}) %>

这是模型中的数组常量:

  class MusType < ActiveRecord::Base
      TYPES = ['Jazz','Rock','Blues']
  end 

我的选择菜单从数组中提取值。在按下提交按钮后,如何将所选值作为参数传递给控制器​​?

1 个答案:

答案 0 :(得分:0)

你可以做下面的事情。
查看代码

<%= form_tag(:url => {:controller => "mycontroller" :action => "display"}, :method => "get") do %>
   <%= select_tag(:music, :type, MusType::TYPES,  {:include_blank => true}) %>
   <%= submit_tag "Search", :id => 'search' %>
<% end %>

读取mycontroller.rb中的选定值

def display
   value = params[:music][:type]
   // do something with value
end