Skip to content

Instantly share code, notes, and snippets.

@alexslade
Created December 9, 2013 17:20
Show Gist options
  • Save alexslade/7876236 to your computer and use it in GitHub Desktop.
Save alexslade/7876236 to your computer and use it in GitHub Desktop.
def create
#fail
@company = Company.new(company_params)
if @company.save
flash[:notice] = "You have successfully created a new company"
redirect_to(:action => 'index')
else
render('new')
end
end
private
def company_params
params.require(:company).permit(:name, :company_number, :tax_number, :address, :city, :state, :zip, :telephone_number, :fax_number, :email_address, :website_address, :facebook_url, :twitter_username, :linkedin_url, :notes)
end
def create
#fail
@company = Company.new(company_params)
if @company.save
flash[:notice] = "You have successfully created a new company"
redirect_to(:action => 'index')
else
render('new')
end
end
private
# Now company_params returns a cleaned-up list of company attributes
def company_params
# this line is the same as before, I'm just storing this in a variable called dirty_params
dirty_params = params.require(:company).permit(:name, :company_number, :tax_number, :address, :city, :state, :zip, :telephone_number, :fax_number, :email_address, :website_address, :facebook_url, :twitter_username, :linkedin_url, :notes)
# Now, defer to another method that will clean up the parameters
clean_params = remove_empty_strings(dirty_params)
# Return the results
return clean_params
end
# This method has one job, can be called by the other actions in this controller if needed. Next step, move this out of the controller so it can be used all over the place.
def remove_empty_strings(params_that_need_cleaning)
# Do something here to loop through your params and replace empty strings with nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment