从 Rails 选择表单获取用户输入
这与上一个问题有关,但我认为它会最好将其分开以便稍后搜索。
我有一个模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尝试过这个。我也尝试采用常量方法。仅当我将
:month
更改为在我的FooModel
中设置的数据库中的列名称后才起作用。在我完成此工作后,我想将其移出 < code>constants.rb 文件并将其放入模型中。 Doug 是正确的,他说我们可以通过 FooModel::Months 访问它,
所以我的最终代码在本实验中如下所示;
模型:
视图:
控制器:
在某些时候,我可以看到使用
constant.rb
来处理常见的事情,如月份、国家等,但对于这个有限的实验,我想以两种方式测试它。如果我使用constants.rb,我的视图将如下所示。查看是否使用constants.rb;
@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 myFooModel
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 viaFooModel::Months
So my final code looks like this in this experiment;
Model:
View:
Controller:
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 theconstants.rb
them my View would look like below.View if using constants.rb;
@hector: hope this helps.
最好为此使用常量。
lib/initializers
中创建一个名为constants.rb
的文件或任何您想要的文件MONTHS
*参见下面的代码In
config/initializers/constants.rb
:在您的视图中:
options_for_select
只是将其格式化为选择输入。It's better to use a constant for this.
lib/initializers
create a file calledconstants.rb
or whatever you wantMONTHS
*see code belowIn
config/initializers/constants.rb
:In your view:
options_for_select
just formats it for the select input.不幸的是,虽然我喜欢 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.