我收到此错误 加载页面时出现此错误。为什么会发生这种情况?这与用户有关吗?我的网址是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'
答案 0 :(得分:1)
我认为您的错误可能来自set_user
中的UsersController
方法,即使您没有在上面显示错误。
你能把它改成:
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
在您的views / users / 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>
如果有效,请告诉我。