Jacob Montgomery


34,500 Experience
33 Lessons Completed
41 Questions Solved


Based on what I see, you're saving the booking, reloading the event, and then checking if it is now over filled. You need to check first if the requested number of spaces is greater than the number of available spaces before you ever call @booking.save!. So you'll want a separate function that gets called in your create function before you save which checks if that event has enough slots available. Also, I'm not sure how you have everything setup, but I would limit the number of bookings a user can select based on the number of slots left at that moment in time as a UX improvement so you don't even let the client think they're going to be able to book that many slots, just to then have the system tell them "sorry, you can't do that!"

You should have a column in your event table that lets you set the total # of spaces available when you're creating the event itself. So to see how many slots are available for that event, you'd just @event.spaces_available. When you're showing all the events, just do something like:

<% @events.each do |event| %>
  <%= event.name %>
  <%= event.date %>
  <% if event.spaces_available == 0 %>
    Sorry, no spaces left!
  <% else %>
    <%= event.spaces_available %> spaces left - book now!
  <% end %>
<% end %>

Anytime you're working with booking events / appointments you need to be aware of race conditions. You need a way to temporarily "reserve" slots while your user is completing the checkout process. There are a ton of ways of handling this, you'll just have to figure out what works best for your setup.

before_action is definitely the way to go! Just treat it like any other method... I use this for a contact form on every page...

before_action :set_contact

 def set_contact
    @contact = Contact.new

then your partial is setup as you'd expect:

<%= form_for @contact, url: user_contact_path do |form| %>

Hah I hear ya - I've been doing web "stuff" for well over 15 years but always stuck to CMS's like Joomla or WP and never really learned how to program, only just how to hack around what someone else did to make it fit my needs... only in the past year did I pickup Rails and boy do I wish I would have started much sooner!

It's all good though, we're here now and as long as Chris keeps pumping out these awesome videos everything will be right as rain! :)

Posted in Devise: Add a select to my signup form

I'm not 100% sure what you're trying to accomplish, but you're looking at either needing to just rename the routes or you may need to override the update method.

If it's just a route renaming issue, it should be something like this:

devise_for :users, :controllers => { registrations: 'registrations', sessions: 'sessions' }, :path => '', :path_names => {:sign_in => 'login', :sign_up => 'register'}

Of course update to whatever your names are...

Check out: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb#L41

Lines 41 - 62 is the update method on the registrations_controller and is what you're going to have to override to redirect to a certain action given whatever conditions you have. I haven't personally done it for this scenario so this is about as far as I can help for this one.

Definitely watch Chris' video on it, I believe that's where I finally figured it all out.

The keys things that stumped me were ensuring

accepts_nested_attributes_for :company

is properly set in the User model, and then ensure your sanitizers are set correct in registrations_controller.rb or application_controller.rb (depending on your setup):

devise_parameter_sanitizer.permit(:sign_up) do |user|
            user.permit(:email, :password, :password_confirmation, :is_brand)

Then in your sign-up form:

<%= form_for resource, as: resource_name, url: registration_path(resource_name),
                             html: { class: "form-horizontal", novalidate: true } do |f| %>

and just call the :is_brand as if it's a normal field..

<%= f.select(:is_brand, options_for_select(Company.is_brands.keys.to_a)) %>

You may need to change your select method - this one is based on an enum being defined in the model to define companies

I think that's all there was to it... good luck! :)

Posted in Devise: Add a select to my signup form

Sure, I'll take a look. I learn as I go so if I've encountered it before then I can probably help!

You may have an issue with Rails 5, I'm still on 4.2.3 for the project I pulled that from. I did encounter your error a lot if I didn't have the proper before_filters set... so for mine its:

before_filter :configure_permitted_parameters, :only => [:create, :update]

Posted in Devise: Add a select to my signup form

What version of devise are you using to have had to move your permitted params into your ApplicationController?
Using Devise 4.2.0 ($ gem list | grep devise) I still have my sanitizers in controllers/registrations_controller.rb and no issue...

def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) do |user|
            user.permit(:first_name, :last_name, :email, :password, :password_confirmation, :avatar, :time_zone)
        devise_parameter_sanitizer.permit(:account_update) do |user|
            user.permit(:first_name, :last_name, :avatar, :email, :password, :password_confirmation, :current_password, :time_zone,
                                    contact_attributes: [:line_1, :line_2, :city, :state, :zip, :phone])

The only thing I don't like is having to declare the sanitizer for the :sign_up action and :account_update separately - it could probably be combined but I haven't dug into it yet (maybe not though??)

Posted in Using multiple theme in Multi tenancy application

I'd suggest making a branch of your project and just test it out. But from the looks of it, it seems like that gem would help you with the organization of your assets but it doesn't seem like there's any real magic going on there that you couldn't just replicate yourself and would save yourself the overhead of the gem dependency.

Read up on how rails handles layouts and the asset pipeline. Also be sure to watch Chris' video on purchased themes.

Posted in Using multiple theme in Multi tenancy application

I believe you could use layouts and get away with something like this...

class ApplicationController < ActionController::Base
  layout :user_theme

  def user_theme

Then each layout you could adjust which css files to use.

Depending on your needs and how you have your site setup, I believe you could also just load a themed css file from the <head>... with a <%= stylesheet_link_tag current_user.theme %>

This should get your thinker tinkering - I'm sure there's a better way and I'm not sure what effect the multi-tenancy could have on any of these ideas...!

Posted in My Development Environment Discussion

If working on OSX, check out Dash for API referencing and snippets - https://kapeli.com/dash - integrates with most major IDE's - searchable, offline mode, etc...

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.