【发布时间】:2012-03-07 18:55:55
【问题描述】:
这是我的 index.html.haml:
= stylesheet_link_tag 'user'
.title
%h1 Port Testing
= form_tag('port_testing/test', method: 'get') do
= text_field_tag :hostname, 'localhost', size: 50
= check_box_tag('Port 80', '80')
= label_tag('80')
= check_box_tag('Port 443', '443')
= label_tag('443')
= check_box_tag('Port 28009', '28009')
= label_tag('28009')
= check_box_tag('Port 2195', '2195')
= label_tag('2195')
%button(type="submit") Test
在我的 routes.rb 中,我有这个:
match 'port_testing/test', :controller => :port_testing, :action=> :test
这是我的 port_testing_controller.rb:
class PortTestingController < ApplicationController
def index
end
def test
puts "\n"
puts @params["hostname"]
end
end
现在当我点击“测试”按钮时,我得到了这个:
开始 GET "/port_testing/test?utf8=%E2%9C%93&hostname=localhost" 获取 127.0.0.1
在 2012-03-07 13:51:33 -0500
由 PortTestingController#test 处理为 HTML
参数:{"utf8"=>"G£ô", "hostname"=>"localhost"}
在 4 毫秒内完成 500 内部服务器错误NoMethodError(你没想到的是一个 nil 对象!
您可能期望有一个 Array 实例。
评估 nil 时发生错误。[]):
app/controllers/port_testing_controller.rb:7:in `test'渲染的 vendor/bundle/jruby/1.9/gems/actionpack-3.1.2/lib/action_dispatch/middl eware/templates/rescues/_trace.erb (6.0ms)
渲染的 vendor/bundle/jruby/1.9/gems/actionpack-3.1.2/lib/action_dispatch/middl eware/templates/rescues/_request_and_response.erb (3.0ms)
渲染的 vendor/bundle/jruby/1.9/gems/actionpack-3.1.2/lib/action_dispatch/middl 救援/布局中的 eware/templates/rescues/diagnostics.erb (155.0ms)
如何将哪些复选框与文本字段中输入的内容一起检查到控制器?
【问题讨论】:
-
@fl00r 你能说得更具体点吗?
-
puts @params["hostname"]转换为puts params["hostname"] -
@fl00r 谢谢!那行得通。我如何将我的 check_box 状态存储在 params 变量中,以便我可以在我的控制器中获取状态?
标签: ruby-on-rails haml