2016-06-29 167 views
1

我將Rails應用程序從3.2.13遷移到4.0.0,但在運行應用程序時,出現錯誤:/signup.html中的NoMethodError未定義的方法`each'for nil:NilClass while running Rails application

Started GET "/signup.html" for 127.0.0.1 at 2016-06-29 17:28:16 +0530 ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by AccountsController#new as HTML Parameters: {"plan"=>"year"} Completed 500 Internal Server Error in 51ms ** [Airbrake] Notice was not sent due to configuration: Environment Monitored? false API key set? true

NoMethodError - undefined method each' for nil:NilClass:
activemerchant (1.47.0) lib/active_merchant/billing/model.rb:11:in
initialize' app/controllers/accounts_controller.rb:83:in load_billing' activesupport (4.0.0) lib/active_support/callbacks.rb:437:in _run__3203841093432473566__process_action__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in
process_action' actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
.......................................

這是我的控制器代碼:

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 

的routes.rb:

get '/signup' => 'accounts#new' 

賬戶/ 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 /計費/ 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 

包括空在上述代碼中的錯誤顯示線。並且沒有model.rb文件用於activemerchant版本1.20.4(我的以前)。 請幫幫我。

+0

從看你的錯誤信息我可以告訴問題是在lib/active_merchant/billing/model.rb:11上的回調。在那條線上,不管你打給誰,每個人都不存在。如果您仍然感到困惑,請從該文件發佈代碼 –

+0

我在問題中已更新。請檢查。 – user3189916

回答

1

ActiveMerchant.new需要屬性及其值的散列(默認爲空散列),但是您傳遞的是導致錯誤的nil。檢查this執行情況以便更好地理解。如果params [:account] [:creditcard]不存在,我認爲你不需要傳遞任何東西(甚至沒有)。

+0

同樣的事情在Rails 3.2中工作。 Active Merchant代碼與版本更改有所不同嗎? – user3189916

+0

@ user3189916我不知道。您將必須檢查您正在使用的版本的文檔。請參閱本教程https://richonrails.com/articles/credit-card-processing-with-active-merchant –

+0

我沒有在我的上一個1.20.4中找到model.rb文件,但它有初始化方法新版本。你能舉出你給出的答案嗎? – user3189916

0

您忘記關閉before_action調用中的方括號。

+0

它已關閉,錯過了它。我更新了我的問題。 – user3189916

2

「include Empty is the error showing line at the above code。」否它引發第11行的異常,即「attributes.each do |key, value|」,因爲屬性爲零。

因此改變這一行:

@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}" } 
=> {} 

但只是讓你明白:

在初始化方法,如果沒有其他的參數是該行

def initialize(attributes = {}) 

初始化與空哈希的缺省參數的類的實例被通過。在你的三元線上:

@creditcard = ActiveMerchant::Billing::CreditCard.new(params[:account].blank? ? nil : params[:account][:creditcard]) 

這個:

params[:account].blank? ? nil : params[:account][:creditcard] 

說如果沒有帳號參數,發送nil作爲參數初始化。在零值將覆蓋默認值({})值,你會得到未定義的方法對每類爲零,因爲你美其名曰:

attributes.each do |key, value| 

,但你已經定義的屬性爲零。

相關問題