Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

Posted in Suggestion: links open in a new tab

Great idea. I need to go update my markdown parser to add target: :blank into the links soon. I'll add that to my todo list. :)

Posted in Advanced routing setup

So the problem with this (as you've already noticed) is that you can no longer assume that the slug references a Product anymore. It might be a product, or it might be a Technology, or it might be an Industry. My personal suggestion would be to use the routes like this instead:

/products
/products/:slug
/industries
/industries/:slug
/industries/:slug/products
/technologies
/technologies/:slug
/technologies/:slug/products

That would let you create nice restful routes for each of the industries and technologies plus the products inside them. The way to think about this is that you start with the most generic thing (Industry or Technology) and then you narrow down into Products inside them.

Now I know while that may be ideal, it might not be a practical solution, so here's how you can merge those into one controller like you are asking.

Normally you have a before_action that sets the product. In this case, you actually want the ones that don't match a product to render the index for a technology or industry. Your controller will first look up the product by name and if it doesn't find anything, you need to try your alternatives.

My solution would basically be this:

  1. Try to find the product. Continue as normal if found.
  2. If no product matches, look up a Technology or Industry.
  3. Once we have a Technology or Industry, let's render a view for the products. This is going to look just like the regular products index at the simplest, but you may want to render different templates for Industry and Technology results instead.
class ProductsController < ApplicationController
  before_action :set_product
  before_action :find_products_if_not_found

  def index
    @products = Product.all
  end 

  def show
  end

  private

    def set_product
      @product = Product.friendly.find(params[:id])
    end

    def find_products_if_not_found
       # @product gets set in the before_action
       if @product.nil?
         @parent = Technology.friendly.find(params[:id]) || Industry.friendly.find(params[:id])
         @products = @parent.products

         # Render the index template if we found a parent for products
         # Since we are in the before_action this should halt rendering of the show action
         render action: :index
       end
     end
end

Basically everything comes in the same route and you start checking each type. If one fails, try the next. Continue doing that until you run out of things to match and then you can let the view fail if there are no matches at all.

Let me know if that helps!

Posted in Other intermediate-advanced ruby/rails websites?

Absolutely! Two of my favorites are RubyTapas (i'm on the homepage!) and Destroy All Software

Posted in File Uploads with Refile Discussion

That'll work. :) I've just exported as a plain CSV similarly because it's hard to tell the encoding of the file sometimes.

As for the Pundit issue, sounds like the variable you're using Pundit against is nil (which is why it looked up NilClassPolicy). Double check that variable is getting set correctly and that should fix it.

Posted in File Uploads with Refile Discussion

Yeah it's very picky. You can google it and lots of people have similar problems. http://stackoverflow.com/qu...

Posted in File Uploads with Refile Discussion

I will definitely record a screencast on this soon! Several people have been asking about it.

Also, check out this blog post that Patrick McKenzie just posted about his advanced CSV upload that he built: http://www.kalzumeus.com/20...

Posted in File Uploads with Refile Discussion

CSV upload is definitely a topic I need to cover soon. I usually don't save the CSV files so I traditionally just use a file_field and let the file get deleted after the request is done in most cases.

Refile could work for storage of the CSVs though and then the model could access the file and parse it as necessary if you want to store the CSVs permanently.

Posted in File Uploads with Refile Discussion

Definitely a worthy discussion to be had with the author of the gem.

Posted in jQuery UJS and AJAX Discussion

I'm using patch because we want to modify the record. You don't ever want a GET request to update a record because that would be unsafe so either a PUT or a PATCH is best in this case.

Posted in File Uploads with Refile Discussion

Yep, shouldn't be a problem. You'll just skip the image processing parts.

Posted in Button Loading Animations with jQuery UJS Discussion

Unfortunately you can't because it doesn't allow HTML inside of it.

Posted in Fragment Caching And oEmbed Discussion

You could remove the expires_in option and set up a nightly cron job to expire the fragments and then immediately request the page instead. When you do it with an expiration like this, you can't know when it expires exactly so it would be hard to expire and then request the page again.

Well shoot. That's definitely missing audio. I may not have the original audio to get that fixed unfortunately. Looks like I'm just explaining that you can set the anchor and your emails can link to and highlight the specific element on the page when you click the link.

Posted in Forum Series Part 6: Search with Ransack Discussion

You can do autocomplete by having it query the URL for JSON response each time someone types a character. My suggestion is to try hooking it up with jQuery UI's autocomplete. http://jqueryui.com/autocom...

Posted in Search functionality for the screencasts

It looks like you're trying to do a string comparison on the created_at timestamp. I think you want to use created_at_gteq instead of created_at_cont in your search form.

Posted in Search functionality for the screencasts

Ah interesting. How come you're going with a custom search?

Posted in Search functionality for the screencasts

Like this? :) https://gorails.com/episodes/forum-search-with-ransack

I can't remember if I talk about going through multiple models, but it's about as simple as making your search field names include the two model names.

Posted in Tracking Rails App Usage with Analytics

That looks super cool. There's also another gem called Ahoy that I've heard good things about but I haven't used it and it doesn't look to have a nice dashboard (from what I saw at a glance).

At One Month, we use segment.io to send metrics to a bunch of different services. One of our most important ones is Mixpanel where we build and measure all our funnels. Admittedly I don't do much tracking other than Google Analytics on GoRails so the data I get back isn't super useful.

I think segment.io is really awesome and you can just tell it to pipe your metrics wherever you want and it gets syndicated for you. All you have to do is build it once and all your services (whatever ones you want) receive the data.

Of course redis_analytics or ahoy make a lot of sense if you want to keep it all internal without using 3rd party services.

Posted in Updating Nested Form / Model Attributes

I just posted an answer that hopefully works for you. The create action is where you want to add the logic to set the role, active, and default_role attributes. Since your form submits a role object already, we can assume this is the last one and update it to have the default attributes of ( :role => 'owner', :active => 1, :default_role => 1 etc.).

Make sense?