Last active
August 1, 2017 03:29
-
-
Save staycreativedesign/0e184957069dde375065928b693ff5e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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
| 1.2) Failure/Error: = form_for @user do |f| | |
| ActionView::Template::Error: | |
| First argument in form cannot contain nil or be empty |
This file contains hidden or 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
| #- 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" |
This file contains hidden or 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 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 |
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
It's because
@useris nil when yourender :new. Even though yourender :new, it doesn't call thenewmethod that runs@user = User.new. You don't really want that anyway, or the form wouldn't keep any of the submitted values.Notably in the above example, there is no
usermethod onCreateUserobjects—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‽)