mercredi 27 mai 2015

Rails + Devise: Creating An Additional User Account Through UI

I have a model setup that allows me to create additional accounts related to the currently logged-in Devise user.

extra_customer_account.rb:

class ExtraCustomerAccount < ActiveRecord::Base

  attr_accessible :email, :customer_id, :first_name, :last_name, :password, :password_confirmation

  belongs_to :customer, :foreign_key => :customer_id

  validates :customer_id, :first_name, :last_name, :email, :presence => true

  def self.create_with_user(email, password, first_name, last_name, customer_id)
    e = ExtraCustomerAccount.create!(email: email, first_name: first_name, last_name: last_name, customer_id: customer_id)
    User.create!(first_name: e.first_name, last_name: e.last_name, email: e.email, password: password, password_confirmation: password)
  end
end

At the moment, I am simply creating additional accounts from Terminal as follows:

$ ExtraCustomerAccount.create_with_user("email@gmail.com", "changeme123", "First_name", "Last_name", 1)

I am now wanting to allow the logged-in user to create additional accounts themselves, without me having to do it through Terminal and struggling to link up the form in the view to hit the method in the model.

If possible, I would prefer to keep the method in the model than duplicate the code in the controller.

extra_customer_accounts_controller.rb:

class ExtraCustomerAccountsController < ApplicationController

  def new
  end

  def create
    @new_customer_account = # What goes here to call the method in model?!
  end
end

Here's my attempt at the view, and I'd like the parameters of first_name, last_name, email and password to the create method in my controller. I'll also need to pass through the currently logged in customer ID.

extra_customer_account/new.html.erb:

<%= form_for @new_customer_account, :url => { action: "create" }, do |f| %>      

    <div class="form-group">
      <div class="controls">                                 
        <%= f.text_field :first_name, :class => 'form-control custom-form-control', :placeholder => 'First name' %>                
      </div>
    </div>

    <div class="form-group">
      <div class="controls">                                 
        <%= f.text_field :last_name, :class => 'form-control custom-form-control', :placeholder => 'Last name' %>                
      </div>
    </div>

    <div class="form-group">
      <div class="controls">                                 
        <%= f.email_field :email, :class => 'form-control custom-form-control', :placeholder => 'Email' %>                
      </div>
    </div>

    <div class="form-group">
      <div class="controls">                                
        <%= f.password_field :password, :class => 'form-control custom-form-control ', :placeholder => 'Password' %>                
      </div>
    </div>            

    <div class="form-actions">
      <button type="submit" class="btn-account-primary">Create Account</button>
    </div>

<% end %>

Would love some further direction as to how I could tie together my view to play nicely with a stripped-down controller and hit the method in model.

Aucun commentaire:

Enregistrer un commentaire