Rails:如何读取用户在text_field_tag 输入中输入的值?
我有一个带有 <%= text_field_tag "mykey" %>
的表单。用户输入myvalue
并提交。当POST请求到达Rails服务器时如何获取这个值?
我可以看到 myvalue
传入 POST 请求:
Started POST "/assessments" for 127.0.0.1 at 2011-07-08 20:04:41 +0900
Processing by AssessmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "mykey"=>"myvalue"}
但是如何在控制器中读取该值?
在AssessmentsController#create
中,我做的第一件事是记录参数,不幸的是它是空的:
logger.debug session[:assessment_params].collect {|k,v| "#{k}: #{v}"}.join
注意:我不能使用text_field代替text_field_tag,因为另一个问题。
I have a form with <%= text_field_tag "mykey" %>
. The user enters myvalue
and submits. How to get this value when the POST request hits the Rails server?
I can see myvalue
passing in the POST request:
Started POST "/assessments" for 127.0.0.1 at 2011-07-08 20:04:41 +0900
Processing by AssessmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "mykey"=>"myvalue"}
But how can I read this value in my controller?
In AssessmentsController#create
, first thing I do is log the params, and it is unfortunately empty:
logger.debug session[:assessment_params].collect {|k,v| "#{k}: #{v}"}.join
Note: I can not use text_field instead of text_field_tag, because of another issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的表单不是模型表单(看起来确实如此),则您只需要
params[:mykey]
。If your form isn't a model form, which appears to be the case, you just want
params[:mykey]
.