Chris Oliver

Joined

292,890 Experience
93 Lessons Completed
295 Questions Solved

Activity

Posted in Nested attributes with collection_select not saving

Are you specifically having trouble saving the records or is the form having trouble rendering?

Posted in Advanced routing setup

Yeah, the find will throw an exception. I think this will work instead because find_by doesn't throw an exception when the record doesn't exist.

before_action :set_product
before_action :find_products_if_not_found, if: ->{ @product.blank? }

private

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

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

         # Since this could be nil, we need to redirect or render a 404 if not found
         if @parent.present?
           @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
         else
           redirect_to root_path 
         end
       end
     end

Posted in Pundit for RESTFUL actions on Model fields

You'll want to set up the strong_params method to point to the Pundit scope for the user. That can return the list of fields you want to allow them to edit since it will be different for different types of users.

Check out this section in their readme. https://github.com/elabs/pundit#strong-parameters

Posted in JSON for Rails delegated attributes?

I'm a big fan of just using jbuilder for this kind of stuff. It makes it rather easy to render out absolutely anything you want and is pretty much just like using the xml builder if you're familiar with that. No need to override to_json or anything. I believe it's intended to do pretty much the same thing as ActiveModel::Serializer, but jbuilder makes it feel more like a view that you're creating. You'll have full access to any instance variables set in the view and should be able to do what you want.

You'll just do this in your controller:

respond_to do |format|
  format.html
  format.json
end

And then create the view file as ACTION_NAME.json.jbuilder.

Posted in Setup Ubuntu 14.10 Utopic Unicorn Discussion

I personally store my configs in a Git repo, clone it on each machine, and symlink the configs.

Posted in Sending emails with Mandrill Discussion

It depends on their SMTP system, I've seen the same delays at times. They might be queuing or batching emails behind the scenes or any multitude of things between them and GMail could be slow. I'm not quite sure.

Just leave that part out. For example:

class NotifierPreview < ActionMailer::Preview
def welcome
Notifier.welcome(User.first)
end
end

Posted in Javascript frameworks videos?

Start a new thread to discuss those features? I like the idea of not just building features but also comparing it to how a production site uses them.

Posted in Introduction to Importing from CSV Discussion

Yeah, it's the items in the row in order. I just used a Ruby trick to split the array of items into separate variables.

irb(main):001:0> a, b, c = ["a", "b", "c"]
=> ["a", "b", "c"]
irb(main):002:0> a
=> "a"
irb(main):003:0> b
=> "b"
irb(main):004:0> c
=> "c"

Posted in Javascript frameworks videos?

The main purpose is for making a website that has a lot of interaction where reloading the page would be intrusive. Look at Facebook and Twitter. If every action you took reloaded the page...it would be really hard to use them.

jQuery can get you pretty far, but it's going to be very disorganize because it isn't built for the same purpose. It's good for smaller things, but when you're building realtime chat like Facebook has, you're going to want something more robust like one of these frameworks.

In a lot of cases, you will be good just using jQuery, Turbolinks, and no full JS framework if you want just want your pages to render faster and to have some decent interactivity. Shopify went with this approach and they're doing really well with it.

Posted in Refactoring Controller Methods Discussion

Haha no worries! :) Feel free to ask questions on the forum too.

Posted in Sending emails with Mandrill Discussion

You can pass in the object into the mailer as an argument. Just set that to an instance variable and render it in your mailer view. If you're using Mandrill templates, you can pass the values over as merge variables instead.

Posted in Storing Constant Date in Rails

If you want them to be editable, then storing them in a model makes sense. You can easily add and update them.

Otherwise if you really want them to be true constants, you could set a constant in Ruby in a Rails initializer file like so:

# config/initializers/countries.rb
COUNTRIES = [
  "United States",
  "Canada",
  #... etc
]

And then you could reference the COUNTRIES variables in your code in other places.

Posted in File Uploads with Refile Discussion

You would create a new "Lesson" record for each each video and have one video upload per Lesson. Do the same as this basically and you're all set. It would be just like creating multiple Film records for the same Actor.

Posted in Deploy Ubuntu 14.04 Trusty Tahr Discussion

There's a typo in the tutorial there. You actually want the .wrapper version commented out in the example config there. I'll get that fixed shortly!

Posted in SimpleCalendar 1.1 released! Discussion

Another option I just thought about is that you could use CSS to hide the weekends (the first and last TDs in each column) and render the normal week_calendar.

Posted in SimpleCalendar 1.1 released! Discussion

Yes, but you would want to create a custom class that inherits from the main calendar. You can take a look at this as an example and adapt it to be a business-week calendar. https://github.com/excid3/s...

Posted in Error Handling in Ruby 4+

I don't think I would consider that being a bad practice. It's going to be nice to have invalid url's redirected to the root. Potentially this is something that Google frowns upon because you're not returning a 404 response though. That might have negative SEO impact potentially.

The other thing this wouldn't handle is missing records. Imagine you have blog posts at /posts/:id. If you visit /posts/asldfjalsdf and a post with that ID doesn't exist, you'd get an error. There's a method called rescue_from that you can use in the controllers to catch from issues like that one. That might help if you want to have a more robust invalid url catching.

I'd say do some research to see if Google doesn't like redirecting to root for missing pages and see if that's a bad thing or not. Maybe someone else here knows too.

Posted in Setup Ubuntu 14.04 Trusty Tahr Discussion

Make sure you run the ruby-build lines before that.

You could skip all this and use apt-get to install Ruby, but the reason we do this is so that when a new version comes out later, you can easily upgrade without losing anything. apt-get will stick you with one version and you won't be able to upgrade it until the Ubuntu developers get around to adding the new version which takes quite some time.

Posted in Forum Series Part 2: Routes Discussion

http://localhost:3000/forum_threads/4/forum_posts is the URL you want to POST to when creating a new forum post. This is what the form's action will point to so that part looks correct.

Your code inside the controller looks correct as well and should redirect back to the forum thread path.

"uninitialized constant ForumThreads" sounds like maybe your module on the controller specified might be with a different spelling? Was it singular :forum_thread in your routes? That would mean you need to do class ForumThread::ForumPostsController if so.