Ruby on rails 运行Rails应用程序时,NoMethodError at/signup.html未定义nil:NilClass的方法'each'
我将Rails应用程序从3.2.13迁移到4.0.0,但在运行该应用程序时,出现了以下错误: 从2016-06-29 17:28:16+0530开始获取127.0.0.1的GET/signup.html ActiveRecord::SchemaMigration加载0.2ms选择 架构_迁移。*从架构_迁移处理 AccountsControllernew作为HTML参数:{plan=>year} 在51毫秒内完成500个内部服务器错误 **[Airbrake]由于配置原因未发送通知:是否监控环境?假API密钥集?真的 NoMethodError-未定义的方法nil:NilClass: activemerchant 1.47.0 lib/active_merchant/billing/model.rb:11:in 初始化“app/controllers/accounts\u controller.rb:83:in” 加载“动态支持4.0.0” lib/active\u support/callbacks.rb:437:in _运行\u 3203841093432473566 \u进程\u操作\u回调的活动支持 4.0.0 lib/active\u support/callbacks.rb:80:in-run\u callbacks' actionpack 4.0.0 lib/abstract_controller/callbacks.rb:17:in 处理行动“actionpack 4.0.0” lib/action\u controller/metal/rescue.rb:29:in'process\u action' 这是我的控制器代码:Ruby on rails 运行Rails应用程序时,NoMethodError at/signup.html未定义nil:NilClass的方法'each',ruby-on-rails,Ruby On Rails,我将Rails应用程序从3.2.13迁移到4.0.0,但在运行该应用程序时,出现了以下错误: 从2016-06-29 17:28:16+0530开始获取127.0.0.1的GET/signup.html ActiveRecord::SchemaMigration加载0.2ms选择 架构_迁移。*从架构_迁移处理 AccountsControllernew作为HTML参数:{plan=>year} 在51毫秒内完成500个内部服务器错误 **[Airbrake]由于配置原因未发送通知:是否监控环境
class AccountsController < ApplicationController
#inherit_resources
ssl_required :new, :create
before_filter :load_billing, :only => [:new, :create]
def new
@account_name = Rails.application.config.custom.accounts.send(params[:account_name]).name
@account_signup_message = Rails.application.config.custom.accounts.send(params[:account_name]).signup_message
rescue
nil
end
def create
@address.first_name = @creditcard.first_name
@address.last_name = @creditcard.last_name
@account.address = @address
@account.creditcard = @creditcard
if @account.new_record?
if @account.save
flash[:notice] = 'Account was created.'
sign_in(@user, :bypass => true)
redirect_to session[:previous_url] || user_reports_path(@user)
else
render :action => 'new'
end
else
@user.account_id = @account.id
if @user.save
flash[:notice] = 'User was created.'
sign_in(@user, :bypass => true)
redirect_to session[:previous_url] || user_reports_path(@user)
else
render :action => 'new'
end
end
end
protected
def load_billing
@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? nil : params[:account][:creditcard]) #This is the line it is showing error.
@address = SubscriptionAddress.new(params[:account].blank? ? nil : params[:account][:address])
end
end
accounts/new.html.erb:
<%= semantic_form_for(@account, :url => account_create_path, :html => { :multipart => true, :class => 'billing'}) do |f| %>
<%= f.inputs :for => :user do |u| %>
#name and email fields are mentioned.
<% end %>
<%= f.inputs :for => :creditcard do |c| %>
#name, card_no, cvv, expire_date etc;
<% end %>
<% end %>
这是我的activemerchant 1.47.0 lib/active_merchant/billing/model.rb:
require "active_merchant/billing/compatibility"
require "active_merchant/empty"
module ActiveMerchant
module Billing
class Model
include Compatibility::Model
include Empty
def initialize(attributes = {})
attributes.each do |key, value|
send("#{key}=", value)
end
end
def validate
{}
end
private
def errors_hash(array)
array.inject({}) do |hash, (attribute, error)|
(hash[attribute] ||= []) << error
hash
end
end
end
end
end
include Empty是上面代码中显示行的错误。activemerchant 1.20.4版在我之前的版本中没有model.rb文件。
请帮帮我。您忘记在操作调用之前关闭方括号。ActiveMerchant.new需要属性散列,它们的值默认为空散列,但您正在传递导致错误的nil。检查实现以获得更好的理解。我认为,如果params[:account][:creditcard]不存在,您不需要传递任何内容,甚至不需要传递nil。上面代码中显示行的错误是include Empty。不,它在第11行引发了一个异常,即attributes.each do | key,value |,因为attributes是nil 因此,改变这一行:
@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? nil : params[:account][:creditcard])
为此:
@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? {} : params[:account][:creditcard])
现在,如果你打电话,你将发送一些不会引发的信息。每个人都在上面
foo = {}
=> {}
2.3.0 :002 > foo.each {|key, value| puts "#{key}=#{value}" }
=> {}
但为了让你明白:
在initialize方法中,此行
def initialize(attributes = {})
如果没有传入其他参数,则使用空哈希的默认参数初始化类的实例。在您的列表中,请在这一行:
@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? nil : params[:account][:creditcard])
这:
表示如果没有帐户参数,则发送nil作为参数进行初始化。nil值覆盖默认的{}值,并且在nil类上获得每个未定义的方法,因为您正在调用:
attributes.each do |key, value|
但是您已经将属性定义为nil 通过查看您的错误消息,我可以看出问题出在lib/active\u merchant/billing/model.rb:11上的回调中。在那条线上,不管你叫什么,每一个都不存在。如果您仍然对问题感到困惑,请发布该文件中的代码。请检查。Rails 3.2中也有同样的功能。活动商户代码是否因版本更改而不同?@user3189916 I donno。您必须检查文档中您正在使用的版本。请参考本教程,我在以前的1.20.4中没有找到model.rb文件,但在新版本中有initialize方法。你能举例说明你给出的答案吗?它可能适用于你的其他情况,因为帐户参数从不为空。
params[:account].blank? ? nil : params[:account][:creditcard]
attributes.each do |key, value|