Chris Oliver

Joined

244,330 Experience
17 Lessons Completed
268 Questions Solved

Activity

Posted in Inviting Users with devise_invitable Discussion

I would add a validation to the TeamMember for that.

class TeamMember
  belongs_to :team
  validate :team_has_owner

    def team_has_owner
      error.add(:base, "Team must have at least one owner.") unless team.team_members.where(role: :owner).exists?
    end
end

Posted in How can I create a user specfic page?

Hey Louie, you may need to explain more of what you're trying to build. Typically you don't want to have multiple Devise models, just a single User with roles instead. It's much easier to work with.

Posted in Tracking Metrics with Ahoy and Blazer Discussion

I don't think a whole lot has changed. They have instructions for using it with Webpacker now, but it seems mostly the same to me. Are you talking about anything specific?

RestClient is pretty good for accessing an external API, although sometimes people have built an wrapper around the API to make it easier to work with. You might check rubygems.org to see if you can find an API wrapper gem.

Then once you get the information back from the API, you probably want to save it in your database.

And to render it in Open Street Maps or something similar, you'd use their Javascript APIs to load in the routes.
https://wiki.openstreetmap.org/wiki/Routing

Posted in How do I manage business hours for a store?

I'm doing this in a table in the form:

 <% I18n.t('date.day_names').each_with_index do |day, wday| %>
            <td class="day">
              <div class="form-group">
                <%= form.select :"#{day.downcase}_opens_at", time_select_options, label: "Open Time", include_blank: "Closed" %>
              </div>

              <div class="form-group">
                <%= form.select :"#{day.downcase}_closes_at", time_select_options, label: "Close Time", include_blank: "Closed" %>
              </div>
            </td>
          <% end %>

That creates selects for each time of day. I have this helper for those times.

module ApplicationHelper
  def time_select_options(step=15)
    tod = Tod::TimeOfDay.new 0
    times = []
    96.times do
      times << [tod.strftime("%l:%M %P"), tod]
      tod = tod + step.minutes
    end
    times
  end

Then it gets serialized by Time Of Day gem in the model.

class Business
  serialize :sunday_opens_at, Tod::TimeOfDay
  serialize :monday_opens_at, Tod::TimeOfDay
  serialize :tuesday_opens_at, Tod::TimeOfDay
  serialize :wednesday_opens_at, Tod::TimeOfDay
  serialize :thursday_opens_at, Tod::TimeOfDay
  serialize :friday_opens_at, Tod::TimeOfDay
  serialize :saturday_opens_at, Tod::TimeOfDay
end

Posted in How to model these relations in Rails

Here's some quick psuedo code:

class Fleet
  has_many :cars
  has_many :trucks

  validate :same_type

  before_create do
    self.fleet_type = "Car" if cars.exists?
    self.fleet_type = "Truck" if trucks.exists?
  end

  private

  def same_type
    if cars.exists? && trucks.exists?
     errors.add(:base, "All items in fleet must be of the same type")
    end
    end
end

Posted in How to model these relations in Rails

Not built-in. You could set it before_create and check the first child model type and use that, and also validate that they're all the same type.

Posted in How to model these relations in Rails

I would probably just add a fleet_type column to the fleet that keeps track of which is allowed: Train Car, etc. Then you can use it for validation against the associated models to make sure they're of the correct type.

Posted in How to get ngrok working with webpack-dev-server

Probably because you're using SSL to access your site over ngrok and your dev server is not. Try using http with ngrok.

Posted in Rails 6.0 + Heroku + Active Storage + VIPS

Whoops, misread Heroku as Hatchbox. The following would be how you would use VIPS on Hatchbox.io:

sudo apt install -y libvips libvips-dev libvips-tools

Posted in How do I manage business hours for a store?

Hey Taylor, I just built this for a project actually so I have some timely experience for ya. :)

Postgres has a Time column you can use that doesn't include a date. This works perfect.

The TimeOfDay gem you can use to serialize those time columns into a TimeOfDay object which is a time without a date. That comes in handy since that's what business hours really are.

We decided to setup 14 columns, a start and end time for each day of the week. You could also save 14 attributes to a jsonb column instead of 14 separate columns. Those two approaches would work about the same and you could query against either one.

As for the form, same as usual. You build a select field that selects a time, and submits it. Make sure the attributes are permitted and the TimeOfDay serialization will make sure it gets cast to a time in the database.

I made a little helper to generate a select options from 12am to 11:45pm every 15 minutes to make selecting times easier.

Posted in GoRails Design

Hey Anton! I bought a theme from Bootstrap's store and used that. 👍

Posted in JWT with Devise (App and API)

Oliver, yep. If you login on the web, you'll login with a session cookie. Otherwise you'll hit the API with a token and be logged in only for that request.

Posted in Unable to create the Rails app in WSL

Sounds like permissions issues and seems like other people have experienced that. Try this: https://askubuntu.com/a/1118158

Posted in Webpack assets doesn't work after deployment

Hmm, good to know. I'm not quite sure why that would be required, but at least you got your problem solved.

I haven't had to do that, but maybe there's something with the way your code is requiring things that makes it need that.

Posted in Webpack assets doesn't work after deployment

Nope, should be nothing to do with Capistrano.

Try running "RAILS_ENV=production bundle exec rake assets:precompile" on the server and confirm it compiles your webpacker assets.

The webpacker:compile step gets appeneded to the precompile, so you never need to run the webpacker command directly.

Posted in Webpack assets doesn't work after deployment

Okay, looks good. I can see the error now.

On your server, is there the matching filename from the error inside public/packs/ ? Seems like the file compiled successfully, so I would check and make sure the file exists on disk. Then you can tell if it's a configuration issue or something else.

Posted in Webpack assets doesn't work after deployment

The JS error link is broken there.

 DEBUG [1885af3e]   yarn install v1.12.3
 DEBUG [1885af3e]   warning package.json: No license field
 DEBUG [1885af3e]   warning No license field
 DEBUG [1885af3e]   [1/4] Resolving packages...
 DEBUG [1885af3e]   [2/4] Fetching packages...
 DEBUG [1885af3e]   info [email protected]: The platform "linux" is incompatible with this module.
 DEBUG [1885af3e]   info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
 DEBUG [1885af3e]   [3/4] Linking dependencies...
 DEBUG [1885af3e]   warning "@rails/webpacker > [email protected]" has unmet peer dependency "[email protected]^1.0.30000697".
 DEBUG [1885af3e]   warning " > [email protected]" has unmet peer dependency "[email protected]^2.2.0 || ^3.0.0".
 DEBUG [1885af3e]   warning "webpack-dev-server > [email protected]" has unmet peer dependency "[email protected]^1.0.0 || ^2.0.0 || ^3.0.0".
 DEBUG [1885af3e]   [4/4] Building fresh packages...
 DEBUG [1885af3e]   Done in 24.61s.

This shows that it's running and compiling your webpack code successfully.

How are you including your packs in your layout?

Posted in Webpack assets doesn't work after deployment

You don't need to link app/javascript/packs. During precompile, it will run yarn and output the files in public.

If you're not seeing the files, then it's an issue with your precompile step. Do your capistrano logs show that it's running the precomile and does that step say anything about webpacker?

Posted in Webpack assets doesn't work after deployment

Like Ivan said, more information is important.

For me, I just make sure Yarn is on my server and it automatically compiles during the assets:precompile step. Simple as that.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.