1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Changed it to that, now I'm getting: undefined method `model_name' for NilClass:Class on the first line.
Is this code in your new action? If not, you might want to try replacing @foo with FooModel.new.
Ah ha, right you are I needed to move that into the right place. Now it's telling me I have a undefined method `select_tag' for #<ActionView::Helpers::FormBuilder:0x4dce8f0> on the f.select_tag line
Updated my post. I was using select_tag instead of select. My bad.
Changed it back and updated it to look like your solution. Getting this on that line undefined method `range' for #<PlayerEventTracking:0x4bb6340>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.