Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Last active August 1, 2017 03:29
Show Gist options
  • Select an option

  • Save staycreativedesign/0e184957069dde375065928b693ff5e5 to your computer and use it in GitHub Desktop.

Select an option

Save staycreativedesign/0e184957069dde375065928b693ff5e5 to your computer and use it in GitHub Desktop.
class CreateJob
def initialize(params, user)
@user = user
@params = params
end
def run!
create_job
end
private
def create_job
@user.create_job(job_params)
end
def job_params
@params.require(:job).permit(:employer, :address, :address2, :city, :state, :zipcode, :contact)
end
end
class CreateUser
def initialize(data)
@user_params = data
end
def run!
create_user
end
private
def user_params
@user_params.require(:user).permit(:first_name, :last_name, :email, :password, :team_id, :phone_number, :birthday, :business_industry_id, :business_category_id)
end
def create_user
user = User.new(user_params)
if user.save
# create user job if successful
true
else
# use this to flash error messages
false
end
end
end
1.2) Failure/Error: = form_for @user do |f|
ActionView::Template::Error:
First argument in form cannot contain nil or be empty
#- users/new.html.haml
%h4
Enter your information
= form_for @user do |f|
= f.label :first_name
= f.text_field :first_name
= f.label :last_name
= f.text_field :last_name
%h4 login details into Bold Networking
= f.label :email
= f.text_field :email
= f.label :password
= f.password_field :password
= fields_for :job do |job|
%h4
Your Employer / Business name
= job.label :employer
= job.text_field :employer
%h4
please select a your business category
= select_tag "business_category", options_from_collection_for_select(BusinessCategory.all, "id", "name"), prompt: "Please select a Business Category"
= select_tag "user[business_industry_id]", options_for_select([])
%h4
Your Employer / Business information
= job.label :adress
= job.text_field :address
= job.label :contact, "Phone number"
= job.text_field :contact
= job.label :address2
= job.text_field :address2
= job.label :city
= job.text_field :city
= job.label :state
= job.select :state, options_for_select(states)
= job.label :zipcode
= job.text_field :zipcode
= submit_tag "Join Bold Networking"
class UsersController < ApplicationController
def new
@business_categories = BusinessCategory.all
@user = User.new
respond_to do |format|
format.html
format.json { render :json => @business_categories }
end
end
def create
@user_creation = CreateUser.new(params).run!
if @user_creation
redirect_to root_path
else
render :new
#flash error message with errors
end
end
end
@nilbus
Copy link

nilbus commented Aug 1, 2017

It's because @user is nil when you render :new. Even though you render :new, it doesn't call the new method that runs @user = User.new. You don't really want that anyway, or the form wouldn't keep any of the submitted values.

  def create
    @user_creation = CreateUser.new(params).run!
    if @user_creation
      redirect_to root_path
    else
      render :new
      @user = @user_creation.user
    end
  end

Notably in the above example, there is no user method on CreateUser objects—you'll have to create that.

If think this is getting complicated, then you'll understand why I initially warned about complexity, and asked whether you thought it would be worth it. 😄 (though maybe it is‽)

@staycreativedesign
Copy link
Author

on render :new
@user_creation is equal to false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment