Chris Oliver

Joined

238,150 Experience
17 Lessons Completed
260 Questions Solved

Activity

Posted in In-App Navbar Notifications Discussion

I appreciate it! :)

Posted in In-App Navbar Notifications Discussion

Hey Dan! I'd change it to something like this: https://gist.github.com/exc...

Posted in GoRails Screencast suggestion: Caching

I do have an episode on Fragment caching that is helpful: https://gorails.com/episodes/fragment-caching-and-oembed

The main reason I probably won't cover query caching is because in order for your website to be always up-to-date, you'll have to query the database to find the most recent records. If you cache queries, you won't know that there are new records since you didn't make another query.

The way fragment caching helps is you make the query (which only takes a couple milliseconds) and then you look at the results to see if there are HTML fragments cached for those results. If so, you just send over the HTML fragment as a response instead. This is a lot simpler and gives you the same benefit in performance.

Posted in Subscriptions with Stripe Discussion

Awesome! I also forgot all about koudoku, so I might have to do an episode on that as well in the future.

You're using rescue without capturing the exception, so you actually lose the error that's happening. If you remove that block, you'll get the exception printed out.

I believe you can't pass a string in there like that to the new method. It only accepts integers http://ruby-doc.org/stdlib-2.3.0/libdoc/date/rdoc/Date.html#method-c-new

Posted in Display user

Hey Nick,

I believe you'll need to actually modify your autocomplete code to render HTML rather than the standard text you pass it.

Here's an example of how you'd include an image in the jQuery autocomplete library: http://stackoverflow.com/a/7896744/277994

Basically you just have to override the default rendering code in the JS so that it can display the image tag rather than the text.

Posted in Subscriptions with Stripe Discussion

Great question and I meant to make a follow up episode talking about that. I will still do that, but here's the gist:

1. You'll need a pricing page that lists the plans. When you click on the link to subscribe, you'll pass the plan ID in the URL to the checkout page.
2. The checkout page will load up the plan based on the ID to verify it exists and is allowed (makes it easy for changing things over time).
3. You'll pass the plan ID as a hidden field during checkout
4. SubscriptionsController's create action will lookup the plan again to verify it and then submits the stripe ID instead of the hardcoded "monthly" value
5. After signing up, you'll link the user to the plan as well so you can use it for your authorization and functionality of your site that they paid for access to.

That should do the trick! I'll try and record a screencast on this very soon as a follow up.

Hmm, that's interesting. I just tried it and it's not doing that for me. There's a user with a birthday in the system as Jan 5, 1980 and it doesn't seem to find nearby results for me.

irb(main):009:0> User.search(where: {birthday: Date.new(1989, 5, 26)}).count
  User Search (2.4ms)  curl http://localhost:9200/users_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"birthday":"1980-01-6"}}]}}},"size":1000,"from":0,"fields":[]}'
=> 0

irb(main):010:0> User.search(where: {birthday: Date.new(1989, 5, 25)}).count
  User Search (2.9ms)  curl http://localhost:9200/users_development/_search?pretty -d '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":[{"term":{"birthday":"1980-01-5"}}]}}},"size":1000,"from":0,"fields":[]}'
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (2, 1)
=> 1

Posted in How to Upgrade to Turbolinks 5 Discussion

It sounds like potentially what you want is the "remote: true" AJAX requests so that you can handle things on your own. Turbolinks won't really do anything here because the form is going to submit a POST and Turbolinks can only really handle GET requests. That's the main reason it's not going to be a good solution for your problem. I think using a JS response to the form submit should give you a lot more flexibility to do what you want there.

The JS console will show you the line of code that was the error in Chrome. That's often the best way of tracking down those little things you need to change.

You might need to modify the CSS to point to the asset-url version of the images since that's a common thing that you'll have to do. 404's won't happen with JS generally because you've got to precompile the files ahead of time, so traditionally this will come from CSS referencing images that are in different locations because of Rails.

Hey Sascha, what's the error you're getting? Normally whitespace like that makes no difference since it's "comma separated" which means that only extra commas would cause it to mess up so I suspect there must be something else going on.

What do you mean by exact match? If you've got some code examples, we can probably help out a bit better. :)

You should just be able to pass over Date objects to handle that accordingly. Integers should also be handled the same way. Normally dates and times get converted to seconds since the epoch, so you may observe that change getting automatically converted in the logs when it queries. I don't think there should be anything special.

You should be able to say this in your HTML to add the url there.
https://gist.github.com/exc...

Posted in Single Devise User Model or Multiple Devise Models?

An example on the forum here would be "Site Admin" (like me) "Moderator" (someone who can clean up things, delete spam, etc) and "Subscriber" (your normal users). They'd each have a bit more access to do things, but they're all the same users at the end of the day with basically no differences on the database level.

Always good to talk those out with someone because if it seems overly complicated, it probably is! :)

You won't necessarily need roles for this because simply the creating of associated records would give you the information if the user is a consumer, a seller, or both.

I would make a User model, a Consumer model, and a Seller model like so:

User
has_one :consumer
has_one :seller

Consumer
belongs_to :user

Seller
belongs_to :user

Then from the User you can check the associations simply enough to determine which type of user they are and give them access to whichever parts of the site you need. You might consider caching a role field on the user for quicker lookups, but that's an optimization you can add later.

The main downside of different models is that you are required to do separate login pages for each (and a lot of confusing links as to who signs in where). Roles take care of this and having just a single User model.

If you've got a bunch of unrelated fields, I would associate the other models with it. So maybe you're building something like Foursquare that has a user, but the user has a personal profile as a reviewer but they also have company profiles for the businesses they own. In that case, you'd just create separate profiles for the companies. Make sense?

Posted in GoRails Markdown and Preview

I'm using RedCarpet + Pygments for the final rendering with syntax highlighting and I'm using the marked Javascript library to render the previews when the text box content changes.

# Markdown previews to comments
class Comment
  constructor: (element) ->
    @element = $(element)
    @commentField = @element.find("[data-behavior='comment-body']")
    @previewArea = @element.find("[data-behavior='comment-preview']")
    @setEvents()

  setEvents: ->
    @commentField.on "change", @handlePreview

  handlePreview: =>
    html = marked @commentField.val()
    @previewArea.html html

jQuery ->
  $.each $("[data-behavior='comment-form']"), (i, element)->
    new Comment(element)
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.