从 Rails 选择表单获取用户输入

发布于 2024-10-15 02:34:07 字数 1177 浏览 6 评论 0原文

这与上一个问题有关,但我认为它会最好将其分开以便稍后搜索。

我有一个模型:

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

我有一个视图:

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

我有一个控制器:

def index
  @foo = FooModel.new
   respond_to do |format|
      format.html # index.html.erb
   end
end

def bar
    @events = params.inspect

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

我尝试通过多种不同的方式访问应该通过下拉菜单传递的值,但它似乎没有被传递。即使调用 params.inspect 也只会将 @events 设置为等于空哈希。我怀疑我的流程出了问题,或者被错误地称呼,或者我把错误的事情称为周期。

当前索引是上面的视图 ^^ 所在的位置,当您单击提交时,它会调用栏。我需要以不同的方式传递变量吗?谢谢。

更新 也许这就是一个线索。我相信我已经了解模型和控件中发生的情况,但视图中仍然有一些问题让我感到困扰。在 <%= select :range, :thing, FooModel::MONTHS%> 中range 和 thing 到底指的是什么?当我在网上阅读教程和此类内容时,我尽可能告诉它们用值填写 html id 和名称标签,但我不知道太多。我需要在任何地方初始化它们吗?它们有具体指代什么吗?

This is related to a previous question but I thought it would be best to split it up for searchability later.

I have a Model:

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

I have a View:

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

And I have a Controller:

def index
  @foo = FooModel.new
   respond_to do |format|
      format.html # index.html.erb
   end
end

def bar
    @events = params.inspect

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

I've tried to access the value that should be getting passed via the drop-down menu a number of different ways but it doesn't seem to be getting passed. Even calling params.inspect only sets @events equal to an empty hash. I suspect something is off with my flow or being called the wrong way or that I'm calling the wrong thing period.

Currently index is where the view up there ^^ lives and when you click on submit it calls bar. Do I need to be passing a variable in a different way? Thanks.

Update
Perhaps this is a lead. I believe I have an understanding of what's going on in the model and the control but something still bugs me about the view. In <%= select :range, :thing, FooModel::MONTHS%> what do range and thing actually refer to? When I read tutorials and such online as best as I could tell these fill out the html id and name tags with values but I don't know much else. Do I need to be initializing them anywhere? Do they refer to anything in particular?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-10-22 02:34:07

我尝试过这个。我也尝试采用常量方法。仅当我将 :month 更改为在我的 FooModel 中设置的数据库中的列名称后才起作用。

在我完成此工作后,我想将其移出 < code>constants.rb 文件并将其放入模型中。 Doug 是正确的,他说我们可以通过 FooModel::Months 访问它,

所以我的最终代码在本实验中如下所示;

模型:

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

视图:

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(FooModel::MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

控制器:

def index
  @foo = FooModel.new
   respond_to do |format|
      format.html # index.html.erb
   end
end

def bar
    @events = params.inspect

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

在某些时候,我可以看到使用 constant.rb 来处理常见的事情,如月份、国家等,但对于这个有限的实验,我想以两种方式测试它。如果我使用constants.rb,我的视图将如下所示。

查看是否使用constants.rb;

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

@hector:希望这有帮助。

I tried this out. I was also attempting to do the constants approach. Which worked only after I changed :month to the name of the column in the DB that was setup in my FooModel

After i got this working I wanted to move it out of constants.rb file and put it in the model. Doug was correct and saying that we could access it via FooModel::Months

So my final code looks like this in this experiment;

Model:

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

View:

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(FooModel::MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

Controller:

def index
  @foo = FooModel.new
   respond_to do |format|
      format.html # index.html.erb
   end
end

def bar
    @events = params.inspect

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

At some point i could see using the constant.rb for common things like Months, Countries etc. but for this limited experiment I wanted to test it both ways. If i use the constants.rb them my View would look like below.

View if using constants.rb;

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

@hector: hope this helps.

许你一世情深 2024-10-22 02:34:07

最好为此使用常量。

  1. lib/initializers 中创建一个名为 constants.rb 的文件或任何您想要的文件
  2. 然后添加常量,在您的情况下 MONTHS *参见下面的代码
  3. 然后您可以通过以下形式访问该常量

In config/initializers/constants.rb:

MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep']

在您的视图中:

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

options_for_select 只是将其格式化为选择输入。

It's better to use a constant for this.

  1. in lib/initializers create a file called constants.rb or whatever you want
  2. Then add you constant, in your case MONTHS *see code below
  3. Then you can access that constant in the form

In config/initializers/constants.rb:

MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep']

In your view:

<% form_for @foo, :url => {:action => 'bar'} do |f|%>
  <%= f.select :month, options_for_select(MONTHS)  %>
  <%= f.submit "Submit", :disable_with => "Submitting..." %>
<% end %>

options_for_select just formats it for the select input.

下壹個目標 2024-10-22 02:34:07

不幸的是,虽然我喜欢 Sam 的答案,并且它确实对我的代码做了一些改进,但它最终并不是我问题的答案。令人沮丧的是,答案是,在我看来,我有一个调试行,我用它来查看控件运行后得到的输出,并且我从错误的变量打印。简单的复制/粘贴监督。

Unfortunately while I liked Sam's answer and it does make some improvement to my code it ultimately wasn't the answer to my question. Frustratingly enough the answer was that in my view I had a debug line that I was using to see what output I was getting after the control would run and I was printing from the wrong variable. A simple copy/paste oversight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文