开发者

Creating a simple drop-down menu in Rails

开发者 https://www.devze.com 2023-02-07 23:29 出处:网络
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|%>

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

精彩评论

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