我正在使用change-ruby gem获取表单参数并通过Change.org API提交。我是Rails的新手,所以我可能没有完全理解如何正确实现这个库并在我的控制器中使用它。
我有一个表格会在#sign
中的ChangeController
操作中发布一些参数。在这个控制器中,我尝试包含change-ruby
gem及其资源。
目前,当我提交表单时,我在尝试创建新的请求对象(uninitialized constant Petitions::ChangeController::Petition
)的行上收到错误:petition = Petition.new(client)
。我错过了什么?
要说清楚,我的应用程序使用的是Engines,这个名称恰好被称为Petitions
。
ChangeController
代码:
# Visit https://github.com/ericisaiah/change-ruby for documentation
require 'change-ruby'
module Petitions
include Change::Resources
class ChangeController < ApplicationController
def sign
client = Change::Requests::Client.new({ :api_key => ENV["CHANGE_API_KEY"], :secret_token => ENV["CHANGE_SECRET"] })
# Get the petition (this is where the error occurs)
petition = Petition.new(client)
# Get the petition id from the Petition URL
petition_id = petition.get_id(params[:petition_url][:value])
# Load the petition
petition.load(petition_id)
# Get the petition auth key
petition.request_auth_key({
:requester_email => params[:requester_email][:value],
:source => params[:page_slug][:value],
:source_description => "Campaign page that is gathering signatures to help the petition."
})
# Submit signature
petition.signatures.add_signature({
:email => params[:sig_email],
:first_name => params[:sig_first],
:last_name => params[:sig_last],
:address => params[:sig_street],
:city => params[:sig_city],
:state_province => params[:sig_state],
:postal_code => params[:sig_zip],
:country_code => params[:sig_country],
:hidden => params[:sig_hidden]
})
end
end
end
答案 0 :(得分:0)
答案很简单:include Change::Resources
行需要在ChangeController类中,否则控制器无法访问它。