Created
February 2, 2013 14:33
-
-
Save brandon-beacher/4697581 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= simple_form_for @sign_up, url: sign_up_path, html: { class: "form-horizontal" } do |form| | |
= form.simple_fields_for @sign_up.customer do |fields| | |
= fields.input :first_name | |
= fields.input :last_name, label: false | |
= form.simple_fields_for @sign_up.user do |fields| | |
= fields.input :email | |
= fields.input :password | |
= form.simple_fields_for @sign_up.customer do |fields| | |
= fields.input :zip_code | |
.form-actions | |
= form.button :submit, class: "btn btn-primary" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SignUp | |
include ActiveModel::Model | |
def customer | |
@customer ||= Customer.new | |
end | |
def customer=(params = {}) | |
@customer = Customer.new(params) | |
end | |
def save | |
user.with_transaction_returning_status do | |
user.save | |
customer.user_id = user.id | |
customer.save | |
@user.persisted? && @customer.persisted? | |
end | |
end | |
def save! | |
user.save! | |
customer.user_id = user.id | |
customer.save! | |
end | |
def user | |
@user ||= User.new | |
end | |
def user=(params = {}) | |
@user = User.new(params) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SignUpsController < ApplicationController | |
respond_to :html | |
def new | |
@sign_up = SignUp.new | |
respond_with @sign_up | |
end | |
def create | |
@sign_up = SignUp.new(sign_up_params) | |
if @sign_up.save | |
flash[:notice] = t("flash.sign_up.create.notice") | |
respond_with @sign_up, location: root_url | |
else | |
flash.now[:alert] = t("flash.sign_up.create.alert") | |
render action: "new" | |
end | |
end | |
private | |
def sign_up_params | |
params.require(:sign_up).permit( | |
customer: [ | |
:first_name, | |
:last_name, | |
:zip_code ], | |
user: [ | |
:email, | |
:password ] ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment