开发者

Creating a simple drop-down menu in Rails

This really seems simple enough yet for some reason I'm missing something critical.

I have a view:

<% form_for :foo, @foo, :url => {:action => 'bar'} do |f|%> 
  <%= f.collection_select :range, FooModel::MONTHS%>
  <%= submit_tag "Submit", :disable_with => "Submitting..." %>
<% end %>

I have a model:

class FooModel < ActiveRecord::Base
  MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep']
end

And I have a controller:

def new
  @foo = FooModel.new
end

def index
  respond_to do |format|
    format.html # index.html.erb
  end
end

def bar
  if params[:foo]
    @foos = params[:foo].inspect
  end

  respond_to do |format|
    format.html # index.html.erb
  end
end

My question is, how do I get at the information as to which combo box element had been selected when the Submit button was clicked? It doesn't seem to be params[开发者_如何学编程:foo], @foo, or anything else I can think of.

Update Looking at it it seems like I should maybe be calling params[:range]? That, however, is nil.


I think your code can be simplified to work this way:

<% form_for @foo, :url => {:action => 'bar'} do |f| %>
  <%= f.select :range, FooModel::MONTHS %>
  <%= submit_tag "Submit", :disable_with => "Submitting..." %>
<% end %>

Using collection_select for simple cases such as this one is probably overkill. f.select should be sufficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜