我在轨道3.0.4上工作。我打算在保存学生记录后向特定号码发送短信。下面我要提到的代码在rails2.X中运行良好,但在rails3.0.4中,我得到了一个错误:
NameError in StudentsController#create
uninitialized constant Student::Net
代码:
def send_welcome_sms
url=URI.parse("http://webaddress.com");
#error occuring at this point
request = Net::HTTP::Post.new(url.path)
message = "message goes here"
request.set_form_data({'username'=>"abc", 'password'=>"xyz", 'to'=> "some number", 'text'=> "#{message}", 'from'=> "someone"})
response = Net::HTTP.new(url.host, url.port).start {|http| http.request(request) }
# If U are Behind The Proxy Comment Above Line And Uncomment Below Line, Give The Proxy Ip & Port
#response = Net::HTTP::Proxy("PROXY IP", PROXYPORT).new(url.host, url.port).start {|http| http.request(request) }
case response
when Net::HTTPSuccess
puts response.body
else
response.body
response.error!
end
end
确保您在控制器中的某个位置有适当的require
语句,或者最好在environment.rb
文件或初始值设定项中:
require 'net/http'