设计用户报名表与业务模型

时间:2013-12-18 18:28:11

标签: ruby ruby-on-rails-3 devise

我正在尝试使用Devise创建用户注册表单,该表单还允许创建与该用户关联的新业务。

我有一个商业模式设置,似乎无法将业务信息保存到数据库。下面是我的代码,我对rails很新,所以如果我问一个明显答案的问题我会道歉。

new.html.erb(用户)

<div class="content">
<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <h1>Sign Up</h1>
        <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
            <%= devise_error_messages! %>
            <div class="form-group">
                <label>Name</label>
                <%= f.text_field :username, :autofocus => true, :class => "form-control", :placeholder => "Full Name" %>
            </div>
            <div class="form-group">
                <%= f.label :email %>
                <%= f.text_field :email, :class => "form-control", :placeholder => "Email" %>
            </div>
            <div class="form-group">
                <%= f.label :password %>
                <%= f.password_field :password, :class => "form-control", :placeholder => "Password" %>
                <p class="help-block">Passwords must be a minimum of 8 characters.</p>
            </div>
            <div class="form-group">
                <%= f.label :password_confirmation %>
                <%= f.password_field :password_confirmation, :class => "form-control", :placeholder => "Retype Password" %>
            </div>

            <!-- Business Infomation -->
            <h2>Business Information</h2>
            <%= f.fields_for :business do |b| %>
                <div class="form-group">
                    <%= b.label :name %>
                    <%= b.text_field :name, :class => "form-control", :placeholder => "Business Name" %>
                </div>
                <div class="form-group">
                    <%= b.label :address %>
                    <%= b.text_field :address, :class => "form-control", :placeholder => "Address" %>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <%= b.label :city %>
                            <%= b.text_field :city, :class => "form-control", :placeholder => "City" %>
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="form-group">
                            <%= b.label :state %>
                            <%= b.text_field :state, :class => "form-control", :placeholder => "State" %>
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <%= b.label :zip %>
                            <%= b.text_field :zip, :class => "form-control", :placeholder => "ZIP" %>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <%= b.label :country %>
                    <%= b.text_field :country, :class => "form-control", :placeholder => "Country" %>
                </div>
            <% end %>
            <div class="well">
                <%= f.submit "Sign Up", :class => "btn btn-primary" %>
            </div>
        <% end %>
    </div>
</div>

user.rb

class User


include Mongoid::Document
  include Mongoid::Paperclip
  rolify
  include Mongoid::Timestamps

  #embeds_many :businesses, :class_name => "Business"

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :as => :admin
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :business_ids, 
  :reset_password_sent_at, :reset_password_within, :address, :city, :state, :zip, :country, :phone, :business_attributes

  has_one :businesses
  accepts_nested_attributes_for :businesses

  validates_format_of :email, :with=>email_regexp, :allow_blank => true, :message=> "Justin"

  #intercom
  attr_accessor :company_name
  attr_accessible :company_name

  ## Database authenticatable
  field :email,              :type => String, :default => ""
  field :encrypted_password, :type => String, :default => ""

  ## Recoverable
  field :reset_password_token,   :type => String
  #field :reset_password_sent_at, :type => Time
  field :reset_password_sent_at, :type => Time

  ## Rememberable
  field :remember_created_at, :type => Time
  #field :remember_created_at, :type => String

  ## Trackable
  field :username,           :type => String
  field :sign_in_count,      :type => Integer, :default => 0
  #field :current_sign_in_at, :type => Time
  #field :last_sign_in_at,    :type => Time
  field :current_sign_in_at, :type => Time
  field :last_sign_in_at,    :type => Time
  field :current_sign_in_ip, :type => String
  field :last_sign_in_ip,    :type => String

  field :first_name,         :type => String
  field :last_name,          :type => String

  #field :business_ids,         :type => Array

  field :address,       :type => String
  field :city,          :type => String
  field :state,         :type => String
  field :zip,           :type => String
  field :country,       :type => String
  field :phone,         :type => String

  # User Avatar 
  attr_accessible :avatar
  has_mongoid_attached_file :avatar,
    :styles => { :full => ["512x512>", :jpg], :medium => ["256x256>", :jpg] },
    :convert_options => {:medium => "-background black -gravity center -extent 256x256"},
    :default_url => "/assets/avatar-blank.png"


  validates_attachment_size :avatar, :less_than => 5.megabytes
  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']


  ## Confirmable
  # field :confirmation_token,   :type => String
  # field :confirmed_at,         :type => Time
  # field :confirmation_sent_at, :type => Time
  # field :unconfirmed_email,    :type => String # Only if using reconfirmable

  ## Lockable
  # field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts
  # field :unlock_token,    :type => String # Only if unlock strategy is :email or :both
  # field :locked_at,       :type => Time

  ## Token authenticatable
  # field :authentication_token, :type => String

  after_create :create_business

  def create_business
    Business.create(business_id: self.id) 
  end

  def assign_default_role(b)
    # assign a default role if no role is assigned
    # IF, invite token make user an editor for business

    # ELSE, make the user owner of the business
    self.add_role "owner", b
  end

  #Returns a businesses for a user. The return type is an array of Business models.
  def businesses
    Business.find(get_business_ids)
  end

  #returns the user business_ids (Array of Strings)
  def get_business_ids
    Business.find_roles(nil, self).map{|b| b.resource_id.to_s}.to_a
  end
end

1 个答案:

答案 0 :(得分:1)

尝试覆盖Devise的注册控制器:

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def create
    # save business here
  end
end 

然后告诉设计使用自定义控制器:

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}