用户中的 NoMethodError#paypal_verification

Posted

技术标签:

【中文标题】用户中的 NoMethodError#paypal_verification【英文标题】:NoMethodError in Users#paypal_verification 【发布时间】:2016-10-18 22:21:50 【问题描述】:

我收到此错误 加载页面时出现此错误。为什么会发生这种情况?这和用户有关系吗??我的网址是http://localhost:3000/users/4/paypal_verification

错误日志:

ActionView::Template::Error (undefined method `verifications' for #<Array:0x007ffc8e78f590>):
    1: <%= form_for([@user, @user.verifications.new]) do |f| %>
    2: 	<%= f.email_field :paypal_email, :class => 'form-control', :placeholder => "Paypal Email", id: 'Pemail' %>
    3: <%= f.text_field :paypal_firstname, :class => 'form-control', :placeholder => "Paypal FirstName", id: 'PfirstName' %>
    4: <%= f.text_field :paypal_lastname, :class => 'form-control', :placeholder => "Paypal LastName", id: 'PlastName' %>
  app/views/users/paypal_verification.html.erb:1:in `_app_views_users_paypal_verification_html_erb___3907504954927390416_70361349334300'
用户控制器
def index

        @search = User.ransack(params[:q])
        @user = @search.result.order("created_at DESC").to_a.uniq

        @country = User.uniq.pluck(:country)
        @city = User.uniq.pluck(:city)
        @school = User.uniq.pluck(:school)
        @major = User.uniq.pluck(:major)


        @results = @search.result
        @arrUsers = @results.to_a

        @revisers = Reviser.all

    end

def paypal_verification

    @user.find(params[:user_id])
end



    def show
        @user = User.find(params[:id])
        @users = User.all
        @revisers = Reviser.all


    end

验证控制器

def create
    @user = User.find(params[:user_id])
    @verification = current_user.verifications.create(verification_params)
    redirect_to @user.edit

    if @verification 

            require 'paypal-sdk-adaptiveaccounts'
@api = PayPal::SDK::AdaptiveAccounts::API.new( :device_ipaddress => "127.0.0.1" )

# Build request object
@get_verified_status = @api.build_get_verified_status(
  :emailAddress => "<%= current_user.paypal_email %>",
  :matchCriteria => "NAME",
  :firstName => "<%= current_user.paypal_firstname %>",
  :lastName => "<%= current_user.paypal_lastname %>" )

# Make API call & get response
@get_verified_status_response = @api.get_verified_status(@get_verified_status)

# Access Response
if @get_verified_status_response.success?
  @get_verified_status_response.accountStatus
  @get_verified_status_response.countryCode
  @get_verified_status_response.userInfo
else
  @get_verified_status_response.error
end

      params.permit!
    status = params[:accountStatus]

verification = Verification.find(params[:paypal_email])

if status == "VERIFIED"
    verification.update_attributes paypal_verified: true
else 
    verification.update_attributes paypal_verified: false
    verification.destroy
end
else
    redirect_to @user.edit
end
    end



    private
        def verification_params
            params.require(:verification).permit(:user_id, :paypal_firstname, :paypal_lastname, :paypal_email, :paypal_verified)
        end

paypal_verification html

<div>
	<h>Hello</h>
<%= form_for([@user, @user.verifications.new]) do |f| %>
	<%= f.email_field :paypal_email, :class => 'form-control', :placeholder => "Paypal Email", id: 'Pemail' %>
<%= f.text_field :paypal_firstname, :class => 'form-control', :placeholder => "Paypal FirstName", id: 'PfirstName' %>
<%= f.text_field :paypal_lastname, :class => 'form-control', :placeholder => "Paypal LastName", id: 'PlastName' %>
<%= f.text_field :paypal_verified, :class => 'form-control', id: 'Pverified'%>

<%= f.submit "verify", class: "btn btn-primary" %>
<% end %>
</div>

routes.rb

 devise_for :users ,
       :path => '' ,
       :path_names => :sign_in => 'login', :sign_out => 'logout', :edit => 'profile',
       :controllers => :omniauth_callbacks => 'omniauth_callbacks',
                :registrations => 'registrations'
              


 resources :users, only: [:index, :show] do

 end
 resources :revisers
 resources :photos
 resources :pages

 #make sure to change this to user!!! instead of reservations!!!
resources :reviser do
  resources :reservations, only: [:create]
end


resources :user do
  resources :verifications, only: [:create]
end


get '/users/:id/paypal_verification' => 'users#paypal_verification'

【问题讨论】:

该错误表明您在数组上调用verifications。我会说你应该检查你的@user 的值。如果您无法弄清楚,您应该发布您的 routes.rb 和完整的 users_controller,尤其是 paypal_verification 操作。 我编辑了我的帖子,我也在使用设计!!不确定这是否会改变事情? 【参考方案1】:

我认为您的错误可能来自您的 UsersController 中的 set_user 方法,即使您没有在上面显示。

你能不能把它改成:

class UsersController < ApplicationController
  before_action :set_user, except: [:index]

  ... your other actions here

  def paypal_verification
   @verification = @user.verifications.new
  end

  private
    def set_user
      @user = User.find(params[:id])
    end
end

在您的意见/用户/paypal_verification.html.erb 中,您可以:

<div>
    <h>Hello</h>
<%= form_for([@user, @verification]) do |f| %>
    <%= f.email_field :paypal_email, :class => 'form-control', :placeholder => "Paypal Email", id: 'Pemail' %>
<%= f.text_field :paypal_firstname, :class => 'form-control', :placeholder => "Paypal FirstName", id: 'PfirstName' %>
<%= f.text_field :paypal_lastname, :class => 'form-control', :placeholder => "Paypal LastName", id: 'PlastName' %>
<%= f.text_field :paypal_verified, :class => 'form-control', id: 'Pverified'%>

<%= f.submit "verify", class: "btn btn-primary" %>
<% end %>
</div>

让我知道这是否有效。

【讨论】:

以上是关于用户中的 NoMethodError#paypal_verification的主要内容,如果未能解决你的问题,请参考以下文章

本地主机中的 NoMethodError:3000

在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?

用户的未定义方法'current_sign_in_ip'(NoMethodError)

CalendarPosts#index 中的 NoMethodError 用于 nil:NilClass 的未定义方法 'title'... 仅有时会出错?

PostsController 中的 NoMethodError#create undefined method `latitude=' "将经纬度保存到连接的表中!!"

PinsController#index 中的 NoMethodError,#<Class:0x0000010150ec98> 的未定义方法“to_a”