Activity
Generally, I put default values in the model itself rather than the controller.
class User
after_initialize :set_defaults
def set_defaults
self.email ||= "email@address.com"
end
end
Posted in Creating usergroups
Hey Trenton,
What you've got is definitely on the right track. The reason being that to create these many to many relationships in SQL is that you have to have the join table in order to have them.
It sounds like you might actually want to put the belongs_to :map
on the Group itself so that you can easily add all the group's users to a map.
Also left you a comment on your SO post. 👍
Posted in How do I add opensearch to a Rails app
Hahaha yeah! :P
I got a chance to read this article: https://aaronparecki.com/2011/07/11/3/how-to-let-google-power-opensearch-on-your-website
It looks like your XML file and stuff should match up with what he manually wrote out. I think you should try pushing to production and load up the XML url and make sure it renders properly with the right urls and everything and see if Google picks it up over the next few days. Pretty sure you've got it setup right.
Posted in How do I add opensearch to a Rails app
That's a great question. I've never intentionally added that feature to GoRails, so I don't actually know. :-) In fact, I don't think there's any OpenSearch tags or anything so I wonder if they can detect a standard like /search url or something in my case.
Posted in What are your goals for 2018?
Hey Alan! Glad you started this again. I love doing these.
2017 was really good. I launched HatchBox.io and having my second product up and running has been fantastic experience.
For 2018, my goals are:
- Launch a 3rd product to help small businesses get started (I'll be announcing this soon!)
- Grow GoRails & HatchBox.io
- I want to explore Crystal Lang a lot more and see about building a new web framework with it
Hey Tatiane,
You can do this pretty easily with an after_create
callback on the StockFlow model.
Roughly, it'd probably look something like this:
class StockFlow
after_create :update_stock
def update_stock
stock = Stock.find_by(name: name)
if stock.present?
case in_out
when "in"
stock.update(amount: stock.amount + amount)
when "out"
stock.update(amount: stock.amount - amount)
end
else
Stock.create(name: name, amount: amount, kind: kind)
end
end
end
I used a thick arrow function which does that for you. You'll see my success function is: "success: (data) => {" and you use "success: function()".
Pretty much exactly what you mentioned. I am working on a video app that I'll be showing off more in the future, but it has a bunch of videos on the page and I wanted to use a Vue app for each one. I just selected all the elements that matched like ".video" and then looped over them and created a new Vue() app for each one. Works like a charm.
You won't need to check if the element exists in this case because it won't run the loop if there are no results so it may be a little more straightforward than selecting by ID which could return you null (and and why we have to check it).
AJAX will always be slower because you're making a second network request and will always force a 'loading' state on the Vue app. I personally hate waiting for widgets to load so I'd rather preload the JSON on the page and have an instantly available frontend.
Posted in Hatch Deploy
You have to setup domains for your apps otherwise your server doesn't know which app to serve and will always just serve the first one.
Posted in Two-factor Authentication
Check out this episode: https://gorails.com/episodes/two-factor-auth-with-devise
Posted in Using Webhooks with Stripe Discussion
Actually, stripe_id on the User model references a Stripe::Customer's ID and stripe_id on a Charge references a Stripe::Charge's ID. That's a great point to clarify!
We're just storing references to the different objects in Stripe so we can load up those objects whether they're a Customer, a Charge, or a Subscription, etc.
Posted in Carrierwave file extension issue
Take a look at this post as well: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Customize-your-version-file-names
Posted in Carrierwave file extension issue
In that case, you're gonna want to change your method for the filename to use .jpg instead of the original extension:
def filename
"#{secure_token}.jpg" if original_filename.present?
end
You might want to add a conditional to do this only for a specific version (the one that you converted to jpg) and use the original extension as necessary for the rest.
Posted in Carrierwave file extension issue
What is your issue?
You have it set to only allow png's:
def extension_whitelist
%w(png)
end
Posted in Passing name objects to Stripe
Hey Charles,
I'm pretty sure you want the description
attribute and you can pass the user's name in there. https://stripe.com/docs/api/ruby#create_customer-description
If you write your Javascript using webpacker, it will use ES6 out of the box.
For the asset pipeline, there's a sprockets-es6 gem.
Posted in Error Tracking with Errbit Discussion
It asks for your repo so it can link you to the line of code that triggered the error.
And yes, any errors from environments mentioned in ignore, will be skipped.
Posted in Why is viewer_id 0 on every video?
You'll want to make sure you have your association setup for it
class Video
belongs_to :viewer, class_name: "User"
end
And then your view can use that association to load the email:
<%= @video.viewer.email %>
Posted in Why is viewer_id 0 on every video?
Hey Michelle,
At a quick glance, you're setting the viewer_id
in your form (which is a database integer column) but your form is submitting email addresses. You probably want to display email address but submit the value as the user's ID instead.
<%= f.select :viewer_id, options_for_select(@users.map{ |u| [u.email, u.id] })), class: 'form-control' %>
If you pass in an array for each user of email and id like this example, it should show the email in the form field, but the ID will be the value actually submitted to the server. That should assign the correct value for you then instead of 0.