尝试通过Devise控制器向payola gem添加订阅功能。当我单击注册页面时,错误消息显示为
Rendered devise/registrations/new.html.erb within layouts/application (7.7ms)
Completed 500 Internal Server Error in 34ms (ActiveRecord: 0.7ms)
NoMethodError - undefined method `plan' for #<User:0x007f8d94acc9d8>:
activemodel (4.2.4) lib/active_model/attribute_methods.rb:433:in `method_missing'
app/views/devise/registrations/new.html.erb:8:in `_app_views_devise_registrations_new_html_erb__2295071264192842435_70123012019120'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.4) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.4) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.4) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/owner/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
responders (2.1.0) lib/action_controller/responder.rb:236:in `default_render'
responders (2.1.0) lib/action_controller/responder.rb:170:in `to_html'
responders (2.1.0) lib/action_controller/responder.rb:163:in `respond'
responders (2.1.0) lib/action_controller/responder.rb:156:in `call'
responders (2.1.0) lib/action_controller/respond_with.rb:203:in `respond_with'
devise (3.5.2) app/controllers/devise/registrations_controller.rb:10:in `new'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
这是Registrations / new.html.erb:
<% content_for :head do %>
<%= render 'payola/transactions/stripe_header' %>
<% end %>
<div class="authform">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form',
:class => 'payola-onestep-subscription-form',
'data-payola-base-path' => payola_path,
'data-payola-plan-type' => resource.plan.plan_class,
'data-payola-plan-id' => resource.plan.id}) do |f| %>
<h3>Sign up</h3>
<div>
<br />
<span id="error_explanation" class="payola-payment-error"></span>
<br />
<br />
</div>
<div class="form-group">
<%= f.label 'Subscription plan' %>
<%= f.collection_select(:subscription_id, Subscription.all, :id, :name) %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, :autofocus => true, class: 'form-control', data: { payola: 'email' } %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<div class="form-group">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil, class: 'form-control', data: { stripe: 'number' } %>
</div>
<div class="form-group">
<%= label_tag :card_code, "Card Security Code" %>
<%= text_field_tag :card_code, nil, name: nil, class: 'form-control', data: { stripe: 'cvc' } %>
</div>
<br />
<div class="form-group">
<%= label_tag :card_month, "Card Expiry" %>
<%= select_month nil, { use_two_digit_numbers: true}, { name: nil, data: { stripe: 'exp-month' } } %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, { name: nil, data: { stripe: 'exp-year' } } %>
</div>
<%= f.submit 'Sign up', :class => 'button right' %>
<% end %>
这是我的注册控制器:
class RegistrationsController < Devise::RegistrationsController
include Payola::StatusBehavior
def create
build_resource(sign_up_params)
plan = Subscription.find_by!(id: params[:user][:plan_id].to_i)
resource.role = User.roles[plan.stripe_id] unless resource.admin?
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
subscribe
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
subscribe
end
else
clean_up_passwords resource
render json:
{error: resource.errors.full_messages.to_sentence},
status: 400
end
end
private
def sign_up_params
params.require(:user).permit(:email,
:password, :password_confirmation, :plan_id)
end
def subscribe
return if resource.admin?
params[:plan] = current_user.plan
subscription = Payola::CreateSubscription.call(params, current_user)
render_payola_status(subscription)
end
end
这是我的订阅模型:
class Subscription < ActiveRecord::Base
include Payola::Plan
has_many :users
validates :stripe_id, inclusion: { in: Subscription.pluck('DISTINCT stripe_id'),
message: "not a valid subscription plan" }
def redirect_path(subscription)
'/'
end
end
这是我的配置/路线:
Rails.application.routes.draw do
get 'content/member'
mount Payola::Engine => '/payola', as: :payola
root to: 'visitors#new'
get 'product/:id', to: 'products#show', :as => :products
devise_for :users, :controllers => { :registrations => 'registrations'}
resources :users
编辑:这是用户模型:
class User < ActiveRecord::Base
enum role: [:user, :admin, :member]
after_initialize :set_default_role, :if => :new_record?
# after_create :sign_up_for_mailing_list
belongs_to :subcription
validates_associated :subscription
def set_default_role
self.role ||= :user
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def sign_up_for_mailing_list
MailingListSignupJob.perform_later(self)
end
def subscribe
mailchimp = Gibbon::Request.new(api_key: ENV["mailchimp_api_key"])
list_id = ENV["mailchimp_list_id"]
result = mailchimp.lists(list_id).members.create(
body: {
email_address: self.email,
status: 'subscribed'
})
Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end
def name
[first_name, last_name].compact.join(' ')
end
端
答案 0 :(得分:1)
你抛弃了很多代码,但没有发布User
模型,这实际上可以提供更好的线索来解决你做错了什么。
你的问题在这里:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form',
:class => 'payola-onestep-subscription-form',
'data-payola-base-path' => payola_path,
'data-payola-plan-type' => resource.plan.plan_class,
'data-payola-plan-id' => resource.plan.id}) do |f| %>
resource
是User
个对象。
您正在尝试访问plan
方法,可能是您定义的Subscription
模型。
在您的用户模型中,应该有:
belongs_to :subscription
你应该这样指代你的subscription
:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form',
:class => 'payola-onestep-subscription-form',
'data-payola-base-path' => payola_path,
'data-payola-plan-type' => resource.subscription.plan_class,
'data-payola-plan-id' => resource.subscription.id}) do |f| %>
我怀疑您的应用程序的其余部分可能存在其他问题(为什么单个订阅会有很多用户?),但这会解决您的一个症状。