RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I've noticed that JQuery file upload supports chunking and resumable uploads. It's hard to find any info on how to do that in Rails. Any hints?

Avatar

Is there any way to make simple_form work nice with ancestry?

I have alerts which have many categories, and categories are nested with ancestry.

Currently I'm using:

<%= f.association :category, :collection => Category.sort_by_ancestry(@organization.categories), :as => :check_boxes %>

But I would like to have categories nested visually.

Avatar

Thanks Ryan. I've been working on a some simple tagging with the native Postgres string-array type this week, it seems to work very well although I don't have any comparative data on performance yet.

https://github.com/dockyard/postgres_ext was a huge help for getting started with it.

Avatar

Do we have XSS vulnerability because of displaying 'raw tags.map'? Hacker could inject some sort of malicious code in a tag name so it would be sent to user without escaping.

Avatar

Bitencode, I am SO glad I stopped by the comments section here after debugging for 3 hours :D thank you!

Avatar
ruby
FB.login(
  function(response) {
    if (response.authResponse) {
      return window.location = '/auth/facebook/callback';
    }
  }, { scope: 'email' }
);

In the above code instead of changing window.location, is there a way to do this through ajax.
Doing this through ajax doesnt seem to work.

Avatar

To completely get rid of the SQL queries issue, you can use staugaard's wonderful kasket gem (that Zendesk also uses, so, you know, probably okay for production :D)

https://github.com/staugaard/kasket

Avatar

If anybody else wants to use caching in their decorators / presenters and use HAML, then this is how to do it:

include Haml::Helpers
def haml_cache(key, &block)
  self.init_haml_helpers
  buffer = haml_buffer.buffer
  h.with_output_buffer(buffer) do
    h.cache key do
      h.concat(yield)
    end
end

end

Avatar

That's a really great episode, thanks Ryan!

Although I am struggling to integrate this with nginx-upload-module.
( https://github.com/vkholodkov/nginx-upload-module/tree/2.2 )

Problem is, we need to have high throughput and are therefore using the nginx module to bypass Rails for uploading files and handle it directly via nginx.

It would be nice if anyone had a suggestion on how to combine the nice client side UI of jquery-file-upload and the backend of the nginx-upload-module ?

Any ideas?

Avatar

Right. See also http://ubuntuforums.org/showthread.php?t=1889595 So, if you are on Ubuntu 12.04 or later, it better to use the 'sudo' group.

Avatar

Thanks for the awesome video, Ryan. Just a note.

jQuery File Upload is great, however it does not handle any sort of file upload for IE since:

a) IE doesn't support drag and drop
b) IE doesn't support selecting multiple files.

In the situation of IE, you just fall back to single file uploads, while things like progress bars do still work.

Edit: Sorry, I should state it will upload multiple files for IE, but you still have to select them one at a time.

Avatar

Adding the ruby//= require private_pub to application.js is missed in the manual

Avatar

Hi, how can I create a new Comment inside my _form.html.erb of any model? I don't want to go to localhost:3000/products/1/tags to create a new tag, I would like to create it inside new.html.erb view of any model. I have problems with the line:

<% form_for [@commentable, @comment] do |f| %>

Avatar

Has anyone gotten this to work with pg 8.4? 8.4 supports hstore but does not support the "CREATE EXTENSION" command. I am not sure what to use instead.

Avatar

Great tutorial and great Gem!

I'm having an issue now that I'm trying to use this in production. in the private_pub.yml I have:

server: "http://mydomain.com:9292/faye"

and I keep getting "failed to load resource http://mydomain:9292/faye" in the javascript console and it doesn't work.

When I lose the port number in private_pub.yml I get a 406 error for faye.js

GET http://mydomain.com/faye.js 406 (Not Acceptable)

Any thoughts on what I'm doing wrong?

Avatar

If you use Kaminari for pagination, there's no need for adding "click" event thru Javascript. Just adding :remote => true in the view works. Check https://github.com/amatsuda/kaminari

Avatar

This one the other hand will work.

ruby
 entry <<  "<media:thumbnail  xmlns:media='http://search.yahoo.com/mrss/' url='#{post.thumbnail}' height='150' width='300' />
        " unless post.thumbnail.blank?
Avatar

I am also running into the same problem - cropping works in Development but not in Production.

In Production, the resizable cropping box does not show up at all. I am running Rails 3.2.8.

@shallwelin do you mind telling me what you did exactly? did you have the same problem I do?

Thanks!

Avatar

Google chart is also a good tool to display live data.

Avatar

@pkohli1 - That's actually how it's supposed to work. Logging out on your app logs out the user from your system but that user should not be logged out of Facebook.

Avatar

there are two GitHub projects which fork Resque and replace Redis with MongoDB:

https://github.com/streamio/mongo-resque

https://github.com/nfo/resque-mongo

This looks pretty awesome!

Not sure which one is more stable and/or feature-complete..

Avatar

there are two GitHub projects which fork Resque and replace Redis with MongoDB:

https://github.com/streamio/mongo-resque

https://github.com/nfo/resque-mongo

This looks pretty awesome!

Avatar

What is the best way of running a worker process on my vps? Is a detached screen session a good idea?
Should i add cron entry to lunch it on restart in case of a restart? Maybe there is a simpler way of doing it?

Avatar

Very helpful, thanks!
If you want to use CSS triangles instead of images:

css
th .asc:after {
  width: 0;
  height: 0;
  content: "";
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #333;
  position: absolute;
  margin-top: 7px;
  margin-left: 5px;
}

th .desc:after {
  width: 0;
  height: 0;
  content: "";
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #333;
  position: absolute;
  margin-top: 7px;
  margin-left: 5px;
}
Avatar

Do you mean like he does in #207? There is even a revised one.

Avatar

Why memcached and not redis, for example?

Avatar

Is there a way to do this when you have nested forms (associations)? As in, we have a @license object, and every @license has @progress_point. Well, the form for licenses looks like this:

ruby
= form_for @license, :url => path do |f|
  .field
    = f.label :name
    = f.text_field :name
  .field
    = f.label :active
    = f.check_box :active
  .field
    = f.label :data
    = f.text_area :data

  %table{:cellpadding => "10"}
    =f.fields_for :progress_points do |builder|
      = render "progress_point_fields", :f => builder


  = link_to_add_fields "Add Sequence", f, :progress_points

The partial "progress_point_fields" has:

ruby
%tr.dynamic_field
  %td
    .field
      = f.label :assessment_id
      = f.text_field :assessment_id
  %td
    .field
      = f.label :milestone_id
      = f.text_field :milestone_id
  %td
    .field
      = f.label :sequence
      = f.text_field :sequence

The problem is that :progress_points isn't an @object so I can't exactly get the ID (or at least I'm not aware of any way of getting it).

Any ideas? Thanks in advance

Avatar

I have a Locations model with an belongs to association to my Areas model. The Areas model is setup with the Ancestry gem where I can build an arbitrary hierarchy of 'Areas' Say like this:

Main Area 1(id = 1)
- Sub Area 1.1 (id = 2)
-- Child Area 1.1.1 (id = 3)
-- Child Area 1.1.2 (id = 4)
- Sub Area 2 (id = 5)
-- Child Area 2.1 (id = 6)
-- etc...

So in my Locations model I have a area_id column and it's easy enough to call Locations.find(1).area.name = 'Child Area 1.1.2' and I can sort easy enough on this.

Now I have also created methods using ancestry functions in the Locations model to return the parent area's like this:

Locations.find(1).area_level_1 = 'Main Area 1'
Locations.find(1).area_level_2 = 'Sub Area 1.1'
Locations.find(1).area_level_3 = 'Child Area 1.1.2'

It's a bit of a hack but work except I can't sort on it. If I call Locations.all I can loop through and display a table etc. on a view and can create a sort function on the last child (i.e. area.name) but sorting based on area_level_1 fails. I am guessing there is an ugly manual SQL way to do this but I was hoping to perhaps find a more elegant way.

Where is really gets ugly is I am trying to leave the structure open enough where the area_id could only be 1 or 2 levels deep (i.e. id = 1 or id = 2 in my example data).

Avatar

I am trying to implement the same thing. Here is my post on stackoverflow:

http://stackoverflow.com/questions/12496333/stange-behaviour-with-twitter-activerecord-reputation-system-average-rating

I tried to follow the same methodology as Ryan has here for the Haiku voting but I must be missing something on my structure.

The 'average' that I get back is not as expected and almost like the it's tracking the previous ratings.

Avatar

Such a great episode!

Can you please guide me how I can go with cancellations, insufficient funds etc..??

Thanks in advanced.

Avatar

I use amCharts. It has some pretty advanced charting features for those needing them. It is a commercial product but well maintained and totally worth it. Available in Flash and Javascript versions.

Avatar

Just a quick note if anyone else gets 'Dalli::RingError: No server available' (or it just says false when you try to write to the cache) I had run it as:

memcached -l localhost

..and then it worked fine.

Avatar

Great episode. One gold tip for me was localtunnel. Awesome and very handy!

Avatar

first of all, thanks... that's just what i've needed now.
But does anybody know why the video content is different from the Show Notes or even the Source Code?

Edit: Sry, didn't see the Notes.

Avatar

Having difficulty creating delete and edit links for the polymorphic object.

Anyone know of a good resource on that?

Avatar

That is because @products.class is Array, which responds_to? :to_csv. I guess you should call to_csv or whatever method name you have as class method: send_data Products.order(:name).to_csv

Avatar

You can also use g Raphael which is the library Raphael provides for drawing charts

Avatar

Should Time.zone.now be in a lambda?

Avatar

Found nvd3 which seems pretty nice.
Quite interesting should also be Dimensional Charting Javascript Library.

Avatar

Sorry, the problem with the above solution is that both the parent and child processes will write out their pid to the worker's pid file. We just want the child so change it to:

ruby
after_fork do |server, worker|
  if Process.ppid > 1 # Not the daemon
    child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
    File.open(child_pid, "wb") {|f| f << Process.pid }
  end
end
Avatar

Add this to the bottom of your unicorn.rb

ruby
after_fork do |server, worker|
  child_pid = server.config[:pid].sub('.pid', ".#{worker.nr}.pid")
  system("echo #{Process.pid} > #{child_pid}")
end

so that each unicorn worker creates its own PID. See this post for more info.

Avatar

Hi guys.
I tried sidekiq out for the first time. It is really cool. But I have a question. Which is the best way to starting sidekiq "bundle exec sidekiq" on server restart.

Avatar

Found this episode really great, specially insight how properly do active record mixins. I'm already using arel specially when queries need some complex includes/joins and SQL doesn't look good in model.
I think Match method is overkill for this case, specially in high performance you would do raw queries for search + caching.