我正在使用rails 3.0.4。我打算在保存学生记录后将短信发送到特定号码。我将在下面提到的代码在rails 2.X中运行良好,但在rails 3.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
答案 0 :(得分:15)
确保您在控制器中,或者最好在require
文件或初始化程序中的某个地方有适当的environment.rb
声明:
require 'net/http'