2013-12-18 53 views
1

我正在嘗試創建一個用戶註冊表單,這也將允許創建與該用戶關聯的新業務。用商業模式設計用戶註冊表單

我有一個商業模式設置,似乎無法將商業信息保存到數據庫。下面是我的代碼,我對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

如果是這樣的Rails 4,你需要允許這些新的參數,請參閱:http://stackoverflow.com/a/17736187/614819 您還需要一些控制代碼來創建新的業務,所以你需要覆蓋Devise的註冊控制器。 – zgohr

+1

你是否重寫Devise的'RegistrationController'?你需要這樣做來允許per @ zgohr的推薦屬性。您還需要使用['accep_nested_attributes_for'](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)爲'business'屬性創建編寫者。這裏有一篇可能有用的文章:http://www.railsexperiments.com/using-strong-parameters-with-nested-forms/ –

回答

1

儘量覆蓋設計的登記控制器:

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

,然後告訴設計使用customi捷思控制器:

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