Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

ac74394's Profile

GitHub User: ac74394

Comments by

Avatar

A little OT, but here's a link to SO with a way to hack Struct into a Rails project. I've just spent the morning reading up on Struct, OpenStruct, etc. and am amazed with the possibilities!

Didn't really know how to approach using OpenStruct or Struct before this episode. Thanks, Ryan!

http://stackoverflow.com/questions/2240535/how-do-i-use-hash-keys-as-methods-on-a-class/4064219#4064219

Avatar

@Alex, thanks a million for putting this together. It makes it WAY easier to connect the dots between this and traditional JS.

Avatar

Hi Brandon - Thanks a million for sharing your valuable insight!

I'll keep honing my Ruby skills for now but will definitely dabble with a client-side .js framework (and it seems that AngularJS is like the RoR of these...)

Avatar

Excellent Railscast, as always!

That said, would anyone be willing to share opinions on when it's appropriate to use these frameworks (Angular, Backbone, etc.) vs. the traditional Rails/AJAX way of doing things? I image the benefit has to be pretty tremendous to undertake the learning curve but it's not apparent to me.

For example, would building the app the traditional way be suboptimal? And why?

I really appreciate the guidance! I'm allocating my excess capacity toward becoming a better Ruby programmer but would be willing to divert resource toward learning a new .js framework if that's a more prudent direction.

Avatar

I ran into an issue when using the ActiveRecord version of this... Specifically, after starting up the server and entering a bunch of search requests (and then doing a bunch of other things on the site,) I began to get a warning in the log complaining about my db connection not being closed properly. This error coincided with every search after that point in time.

I think this may be because the middleware is inserted prior to ActiveRecord::ConnectionAdapters::ConnectionManagement (which is responsible for opening and closing DB connections.) ???

So, I inserted the middleware after the ConnectionManagement class and it seems to be working now.

Avatar

Love the example! I also think polling is a viable solution for many applications. Unfortunately, I'm not a big fan of coffee script so JS based episodes like these require a bit more focus on my part...but maybe that means I should start using coffee script?

Avatar

Thanks for this episode! Turbolinks is nice. It's like ajax loading on steroids. Probably would need lots of finessing to make it seamlessly work on a large site but I like the direction here anyway.

Avatar

I'm fairly new to all of this but yes, I believe caching the template renders any erb conditionals contained within the cached portion useless (except for the first time the cache is created.) You can get around this, using JS and HTML5 data attributes:

First, create your 'sensitive link' - in this case, we'll wrap it in a div named delete-btn - and use CSS to HIDE the link. Then, assign a data attribute to the div:

<div class="delete-btn" data-admin="<%= user.admin? %>">

Of course, this assumes the User model has an admin attribute.

Then, it's as easy as fetching the admin attribute using JS and writing a quick if statement that checks if the admin data attribute is true. If so, unhide the link.

Angelo

Avatar

Very handy episode! How about one on caching techniques using Memcached, etc? Russian Doll caching seems to be something interesting as well...

Avatar

Pushing off processing to a background task using DelayedJob seems like a worthy experience!

Avatar

Thanks -- I am aware of the episode's show notes. I was referring to the code that Ryan submitted to Rails Rumble. That's not included in the show notes!

Avatar

This is great, Ryan! Your stamp project looks really neat! Is the code available? If so, can you link to the show notes?

Avatar

+1... I am hoping the next episode will be "Refactoring Virtual Attributes (and eliminating callbacks.)" I've been reading up a lot on this and think an episode will solidify the concepts!

Ryan rocks.

Avatar

Very useful screencast and I was interested in this when T-Love originally posted.

Anyone know if attr_accessible is considered a class variable (i.e., requiring a mutex lock if I enable Threadsafe!)?

Example --

I have a Post model that dynamically sets attr_accessible from the Posts controller based on a user's permission.

The controller action first looks up the user and then looks up the user's permissions (for example, an admin user would have all fields accessible.) Would I wrap this in a mutex? Any idea how to even test concurrent action calls with more involved than just hitting a controller action with a generic get (i.e., a put or post with data...)?

Avatar

Wow, this is really great! Very nice screencast, Ryan; it's neat to see how easily it can be done!

A little OT, but I was amazed at the number of columns and amount of data in the User table. They pretty much keep everything there...At first I thought that would be a tremendous amount of overhead to get that much data every time you want to query a simple user attribute...?

But, on second thought, they probably store the entire User table in some gigantic memory cache.

Anyway, neat stuff!

Avatar

This is great...I was just looking into a db schema for the other side of your nested model example (where results would be saved and the user would have a chance to edit the results.) HSTORE seems like it would be a good way to store all the survey responses.

I was going to serialize all the question responses and store them in a column of a table named survey_responses but it looks like HSTORE is a more robust solution!

Avatar

I actually feel good about spending the $9 per month for episodes like this! That's a big statement considering how cheap I am.

Avatar

Nice 'cast...

Can anyone share an Apache2 config block that directs Apache to serve the static gzipped assets like nginx? The Rails guide is short on the topic...and my skills with setting up Apache config files are not good.

Oh, and there is a good writeup on SO that shows how to package up your assets to serve on a conditional basis (e.g., to specific versions of ie, etc.)

http://stackoverflow.com/questions/7134034/using-rails-3-1-assets-pipeline-to-conditionally-use-certain-css

Thanks!

Avatar

I decided to use Kaminari in one of my projects I am updating to Rails 3.1. Just in case anyone has SEARCH functionality in their application, the original code probably looks like this:

if params[:search].nil?
@users_paginated = [].page(params[:user_page])
elsif params[:search].blank?
@users_paginated = [].page(params[:user_page])
else
@users_paginated = user_search_method # this is just a method I created for my project.
end

Your new code should probably look like this:

if params[:search].nil?
  @users_paginated = Kaminari.paginate_array([]).page(params[:user_page])
elsif params[:search].blank?
  @users_paginated = Kaminari.paginate_array([]).page(params[:user_page])
else
  @users_paginated = user_search_method # this is just a method I created for my project.
end

OR, if you wanted to return the first group of users in the database when no param is entered or if the result is blank, you could do the following:

@users_paginated = User.page(params[:user_page]).per(10)  # This would return the first 10 users.

I hope this helps someone.

Avatar

I hope this (i.e., PayPal) is offered as an option in the near future to purchase a Railscasts Pro subscription!

Avatar

@dicky - It sounds like the cookie is always being set as permanent. So, I'd check the 'if' statement that sets the cookie to and make sure it's actually doing what you'd like it to do. I'm sure if you post the code (here, or for example, on RailsForum,) someone will help you out.

Avatar

Regarding validations...

Well, if you're like me, you have password validation and also encrypt the password before saving the user model.

That's a big monkey wrench for Ryan's excellent solution above.

To get it all to work, I created a PasswordReset model (the only way I know of to avoid the before save calls on the user model). I used almost all of Ryan's code but had to make a few modifications.

Then, I had to modify the password_resets controller to make use of the new model. I also moved the 2.hours_ago check to the edit action because it improves the user experience (oh, and I also plan on running a weekly job to remove old password reset tokens)

All-in-all it works pretty well (in need of some refactoring, though because I think the controller is too fat...) Here's the code - I hope it helps!

''' ruby

class PasswordResetsController < ApplicationController
def new
end

def create
user = User.find_by_email(params[:email])
if user
user.create_password_reset
UserMailer.delay.password_reset(user)
end
redirect_to root_url, :notice => "Email sent with password reset instructions."
end

def edit
password_reset = PasswordReset.find_by_password_reset_token(params[:id])
if password_reset.nil?
redirect_to root_path
return
else

@user = password_reset.user
if password_reset.created_at < 2.hours.ago
flash[:notice] = "Password reset has expired. Please try again"
redirect_to new_password_reset_path
end
end
end

def update
@user = PasswordReset.find_by_password_reset_token!(params[:id]).user
if @user.update_attributes(params[:user])
redirect_to root_path, :notice => "Password has been reset!"
else
render :edit
end
end
end
'''

Avatar

Hi Ryan - nice 'Casts!

A suggestion/request for Railscasts: Any way you can add a link in each episode's "show notes" page to that episode's git source code page? That would make what is already an awesome resource even better!

Avatar

Thanks, Ryan! I just finished building this identical functionality into one of my projects. Unfortunately, I'm still a beginner so I had to go the elementary route (essentially looping thru each parent comment and for each of them, looping thru the child comment). It works very nicely but I think I like your way of doing it better!