在Rails中创建一个简单的

在Rails中创建一个简单的

本文介绍了在Rails中创建一个简单的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来确实很简单,但是由于某些原因我错过了一些关键的东西.

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

我有一个观点:

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

我有一个模特:

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

我有一个控制器:

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

我的问题是,当单击提交"按钮时,如何获得有关选择了哪个组合框元素的信息?似乎不是params [:foo],@ foo或其他我能想到的东西.

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.

更新看着它,看来我应该调用params [:range]了吗?但是,那是零.

UpdateLooking 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 %>

在诸如此类的简单情况下使用collection_select可能会过大. f.select应该足够.

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

这篇关于在Rails中创建一个简单的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:46