2

我正在构建一个涉及为客户注册不同计划的 Rails 应用程序。我正在使用 Rails 以及宝石 Devise 和 Stripe 以及 Figaro。我有三个计划,plan_id 1 将成为管理员并且是免费的。plan_id 2 是基本的,免费的,允许客户查看网站但不能互动,而 plan_id 3 是基于订阅的。所有表格都可以正常工作,我可以注册,但是在控制台中没有一个用户分配了plan_id?他们的电子邮件地址和密码被存储,但没有别的。请帮忙,我找不到问题!

页面控制器

class PagesController < ApplicationController
    def home
        @admin_plan = Plan.find(1)
        @partner_plan = Plan.find(2)
        @paidsubscription_plan = Plan.find(3)
    end
end

注册控制器

class Users::RegistrationsController < Devise::RegistrationsController

  before_filter :select_plan, only: :new

  def create
    super do |resource|
      if params[:plan]
        resource.plan_id = params[:plan]
        if resource.plan_id == 3
          resource.save_with_payment
        else
          resource.save
        end
      end
    end
  end

  private  
  def select_plan
    unless params[:plan] && (params[:plan] == '1' || params[:plan] == '2' || params[:plan] == '3')
      flash[:notice] = "Please select a valid membership plan."
      redirect_to root_url
    end 
  end
end

模型/用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   belongs_to :plan
   attr_accessor :stripe_card_token
   def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
  end
end

架构.rb

ActiveRecord::Schema.define(version: 20150820123651) do

  create_table "plans", force: :cascade do |t|
    t.string   "name"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "plan_id"
    t.string   "stripe_customer_token"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

/views/devise/registrations/new.html.erb

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <div class="well">
      <% if params[:plan] == '3' %>
        <%= render 'paid' %>
      <% else %>
        <%= render 'basic' %>
      <% end %>


      <%= render "devise/shared/links" %>
    </div>
  </div>
</div>

一个基本的 ../views/pages/home

<h1>Home Page</h1>

<div class="col-md-4">
   <%= link_to "Admin Sign-Up", new_user_registration_path(plan: @admin_plan.id), class: "btn btn-info btn-lg btn-block" %> 
</div>
<div class="col-md-4">
   <%= link_to "Partner Sign-Up", new_user_registration_path(plan: @partner_plan.id), class: "btn btn-warning btn-lg btn-block" %> 
</div> 
<div class="col-md-4">
   <%= link_to "Paid Subscription Sign-Up", new_user_registration_path(plan: @paidsubscription_plan.id), class: "btn btn-success btn-lg btn-block" %> 
</div>

/views/devise/registrations/_paid.html.erb

<h2>Sign up </h2>
      <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="field form-group">
            <%= f.label :email %><br />
            <%= f.email_field :email, autofocus: true, class: "form-control" %>
        </div>

        <div class="field form-group">
            <%= f.label :password %>
            <% if @minimum_password_length %>
            <em>(<%= @minimum_password_length %> characters minimum)</em>
            <% end %><br />
            <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="field form-group">
            <%= f.label :password_confirmation %><br />
            <%= f.password_field :password_confirmation, autocomplete: "off", 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" %>
        </div>

        <div class="form-group">
            <%= label_tag :card_code, "Security Code on Card (CVV)" %>
            <%= text_field_tag :card_code, nil, name: nil, class: "form-control" %>
        </div>

        <div class="form-group">
            <%= label_tag :card_month, "Card Expiration" %>
            <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
            <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
        </div>

        <div class="actions form-group">
          <%= f.submit "Sign up", class: "btn btn-info" %>
        </div>
      <% end %>

/views/devise/registrations/_basic.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="field form-group">
          <%= f.label :email %><br />
          <%= f.email_field :email, autofocus: true, class: "form-control" %>
        </div>

        <div class="field form-group">
          <%= f.label :password %>
          <% if @minimum_password_length %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %><br />
          <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="field form-group">
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="actions form-group">
          <%= f.submit "Sign up", class: "btn btn-info" %>
        </div>
      <% end %>

路线.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'users/registrations' }

  root 'pages#home'

/config/application.yml

stripe_api_key: sk_test_bCKoKAs1o2jciSJ6Egi4i5tr
stripe_publishable_key: pk_test_q8xRzIcHxx2NXmAhb2yxKxv5
#
# production:
stripe_api_key: sk_test_bCKoKAs1o2jciSJ6Egi4i5tr
stripe_publishable_key: pk_test_q8xRzIcHxx2NXmAhb2yxKxv5

  • 编辑 *

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :stripe_card_token, :email, :password, :password_confirmation) }
    end
end

以下是我尝试注册基本会员时的服务器

Started POST "/users" for 124.149.46.152 at 2015-08-21 02:39:09 +0000
Cannot render console from 124.149.46.152! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"b70CPfotJ27nM7Sz6SXs1iZzzct3gJcouickmCRgaFdF8N/tI/5XuIXgu0dXnOysifkaRIvjfGxFV6lx9UcbNA==", "user"=>{"email"=>"partnertest@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'partnertest@test.com' LIMIT 1
  SQL (0.6ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "partnertest@test.com"], ["encrypted_password", "$2a$10$DMHp9/uWFU/ChVX5s485deASl9T4x48YUVZ6QgJpsw2O9M.A4R6PS"], ["created_at", "2015-08-21 02:39:09.274684"], ["updated_at", "2015-08-21 02:39:09.274684"]]
   (9.5ms)  commit transaction
   (0.1ms)  begin transaction
  SQL (0.7ms)  UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["last_sign_in_at", "2015-08-21 02:39:09.288182"], ["current_sign_in_at", "2015-08-21 02:39:09.288182"], ["last_sign_in_ip", "124.149.46.152"], ["current_sign_in_ip", "124.149.46.152"], ["sign_in_count", 1], ["updated_at", "2015-08-21 02:39:09.289989"], ["id", 6]]
   (11.8ms)  commit transaction
Redirected to https://socialplayground-portal-runpixelrun.c9.io/
Completed 302 Found in 191ms (ActiveRecord: 23.1ms)


Started GET "/" for 124.149.46.152 at 2015-08-21 02:39:09 +0000
Cannot render console from 124.149.46.152! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PagesController#home as HTML
  Plan Load (0.3ms)  SELECT "plans".* FROM "plans"
  Rendered pages/home.html.erb within layouts/application (0.6ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 6]]
Completed 200 OK in 149ms (Views: 146.9ms | ActiveRecord: 0.6ms)

谢谢

4

0 回答 0