RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I had this problem when running on the same machine in production environment, because the assets are precompiled, and thus are served twice.
You should change the assets dir on the dev environment + bundle exec rake tmp:clear to clear the precompiled assets.

Avatar

and if we wanna handle the validation after submit the form in a case there is some errors? whta to do? someone have a nice tutor about it?

Avatar

There should be a revised episode for this, or are there better options to use nowadays?

Avatar

Hi guys, nice episode.
Ryan, I think that the following code could help handling validation errors.

ruby
<%= f.hidden_field :_destroy, :value => f.object._destroy %>
javascript
$("input[type=hidden][id$='_destroy'][value='true']").closest("ITEM SELECTOR").hide();

What do you think?

Thanks,
Mauro

Avatar

I really find it useful! But I'd also like to know how'd you test it.

Avatar

Hi all...

I've tried to execute this railscast but i received this error.

failed: "sh -c 'cd /home/webadmin/apps/apptest/releases/20121002133503 && bundle install --gemfile /home/webadmin/apps/apptest/releases/20121002133503/Gemfile --path /home/webadmin/apps/apptest/shared/bundle --deployment --quiet --without development test'" on 10.X.X.X

It seems the copy cache is not copied in the release folder...

Thanks for your help

Avatar

Has anyone got a monit template for Sidekiq and/or Redis?

I'm having a bit of a hard time setting the pidfiles up correctly with Unicorn Zero Downtime Deployments. This is where I'm at right now https://gist.github.com/3818705

Thanks

Avatar

You're thinking of match, which matches a route to a thing. This is "mount" which mounts a thing at a route. It's reversed because the subject/object of the two verbs are reversed.

Avatar

actually i cannot find a solution for this in the link i sent. have you solved it?

Avatar

Thanks for sharing this ! It turns out there is a transition between the paginator pages line is being shown and removed for adding next page results. And my client complained a lot for this.

Avatar

A note on the destroy action: You should really use Rails' dom_id helper method here because AFAIK that's what all of Rails' other helper methods (e.g. content_tag_for, form_for etc.) use. So the code would be like this:

javascript
$('<%= dom_id(@task, :edit) %>').remove();
Avatar

I'm getting 'Graph Placeholder not found' despite the div id being on the page. Any ideas?

Avatar

I guess Google would index the endless page as multiple pages (products?page=1, products?page=2) by following the links in the will_paginate nav.

If Google then directs a user to, say, /products?page=2 then that user would have no way to conveniently navigate to the content on page 1. Has anyone tried addressing this, perhaps with two-directional endless scrolling (when user scrolls near top of page we load in previous records if they exist), or by redirecting to page 1 if a JavaScript-enabled browser requests any other page?

Avatar

Put it before your render ;)

ruby
headers["Content-Disposition"] = "attachment; filename=\"#{filename}\"" 
Avatar

Great screencast. Will the grouped_collection_select work for a many to many relationship with multi-select? Any examples? Thank you.

Avatar

Thanks for clarifying! Works as you said.

Avatar

This fixed a TONNE of problems for me! I tried everything but this worked! Grrrrr....

Thanks very much :)

Avatar

Okay, I found the solution to my first problem. I didn't include the .yml translation file, which you can see here:

https://github.com/ernie/ransack/blob/master/lib/ransack/locale/en.yml

But I still haven't figured out why the add_field doesn't work...

Avatar

x-tmpl is a dead project. This episode would have been better without using that template language. I would rather use jQuery file upload plugin with HTML5 and CarrierWave to implement the multiple file uploads with progress bar.

Avatar

Any hints on why not to use dom ready? Links?

Avatar

Thank you so much. Basics for some, perhaps, but I such a solid and great explanation, this was very helpful.

I also appreciate your using regular JavaScript/JQuery for the example, rather than CoffeeScript; the sleep() tip is nice as well :]

Kudos for this!

Avatar

it basically 'overlooks' the validation you have in your user model

Avatar

Considering that Morris is Javascript (and thus doesn't generate graphs server-side), it seems disappointingly complicated to use in the sort of situations demonstrated in this episode.

Specifically, since the data to be graphed is already present in the markup, it would be nice if Morris could be instructed to create the chart directly from that tabular data as represented by the markup itself. This would reduce duplication by not having specific Rails code for generating Morris-specific data, as well as removing clutter from the rendered markup which can make it more cumbersome to do certain debugging tasks, and which is only useful to Javascript anyway (and not, e.g. screen readers).

The above could imply a situation in which all of the data is present in the markup--as opposed to say, the latest data only, with the chart perhaps a representation of a longer time-frame--but as it is, Morris still would need all of that data to be present in the markup anyway (albeit in a data attribute). This could be solved nicely via AJAX, and perhaps fetching sampled data (if the data set is large), but such a request could still respond with a document fragment containing tabular markup, and thus still removing any need for separate chart-rendering code (but possibly adding a small amount of complexity for specifying things like sample resolution). This would also be a reasonable approach when only the chart itself is meant to be shown.

It's (sadly) out of date, but there was a good Javascript library that could in fact take data directly from the markup, and used the canvas to render its output: Bluff, isn't visually polished enough by today's standards, but is a good example of such functionality in action, and really and truly is easy to use.

Avatar

To remedy this issue, I added two more routes, just after the one added in the tutorial to handle the 'PUT' and 'DELETE' options for update and destroy. I've only played around with it for a little while, but I haven't seen any issues. This is how my routes for the pages look:

ruby
root to: "pages#index"
get ':id', to: 'pages#show', as: :page
put ':id', to: 'pages#update', as: :page
delete ':id', to: 'pages#destroy', as: :page

EDIT: Just for further clarification, the paths to edit, would be http://<yourapp>/pages/<page>/edit, rather than http://<yourapp>/<page>/edit. I haven't worked around that yet, but haven't really tried either.

Avatar

Also, remember to do not use, jQuery $(function(){}); (document ready), just leave your js file at the bottom, which is the best practice.

Avatar

.on() is the preffered way to add events on new elements.

Ppl used to rely on .live() which would parse the whole page for all elements matched in the selector. This was causing heavy client side cpu usage.

Avatar

Seeing how to writing a plugin is nice, sure, but its just not the best solution here imo. Using "on", you don't need to worry about adding any event handlers later.

Avatar

Also, the admin group didnt exist on the Ubuntu 12 VPS that I used. So the useradd failed. I ended up doing:

default.rb
group "admin" do
  gid 420
end

user "deployer" do
  password "$%^&shadowhash&$%&#*(."
  gid 420
  home "/home/deployer"
  supports manage_home: true
end

This worked for me. But anyone know if this is because of a difference in distros from the one used on the Railscast? Is admin usually a pre-created default group?

Avatar

Even after installing the above packages and finishing the ruby/chef installation process on the Ubuntu 12.04 VPS, I would get an error on launching 'chef -v':

Terminal
"/usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- openssl (LoadError)"

I ended up doing this to install ruby and chef:

Terminal
apt-get update
apt-get install make ruby1.9.3
gem install chef ruby-shadow --no-ri --no-rdoc
Avatar

Perhaps because he wanted to demonstrate how simple it is to write "jQuery Plugins"?

Your solution is admittedly more mainstream, but is it more generic?

Avatar

Hi guys, does anyone have a good resource on creating multiple user plans and subscriptions? I've followed Michael Hartl's tutorial, and am on to my own app, but I don't know where to begin with multiple user roles.

Most of the application is already written. There are four different plans and users can sign up for any one of them to create a subscription. There’s only an email field on the sign-up page right now and when we fill that field in and submit the form a new subscription will be created.

Avatar

Thanks - will check them out :)

Avatar

+1, thought so too (somebody mentioned it higher up in the comments already though).

Avatar

really bummed ryan didn't show the best way to handle events bound to new elements added to the dom. Although it seems simple to add a function the better and more generally accepted best practise would be to use the .on event Delegation. example:

javascript
$(function() {
  $("body").on("click", "p", function(){
   // do the stuff
    });
});
Avatar

First of all thanks for the great Episode. But I have two problems:

1) The Dropdown List of how to filter is abbreviated. As example it doesn't say "greater than or equal", but instead says "gte" and I didn't find any configurations on how to change this.

2) And this is a real problem: The javascript code won't work. I can only remove a field, but not add one. So I will always have at least one field, but I can never add one. I tried the Coffeescript code with Barista in my 3.0 app and also translated it to jQuery and tried if with that, but it never worked. Any ideas?

Avatar

In case anyone is interested: I just released a gem for "ajaxifying" a rails app: https://github.com/ncri/ajaxify_rails

Avatar

One issue could be speed. The latency for sites to get to our users is quite significant when hosting in the US and users are in Australia.

Avatar

For some reason the chef_solo_bootstrap.sh didnt work for me. I had to end up installing the following packages:

apt-get install libreadline-gplv2-dev lib64readline-gplv2-dev make zlib1g-dev

Avatar

I created a simple gem that extracts out the form helper and cofeescript found in Ryan's rails app he mentioned at the end. You can find it here:

https://github.com/BlissOfBeing/s3_direct_upload

There aren't many tests for it yet, but hey it works for now. Would be grateful for any pull requests ;)

Avatar

Noel Rappin has a series of self published books on Javascript and he happens to use Rails as the server. They are fairly decent:

http://www.noelrappin.com/

Hope this helps

Avatar

Thanks. It works fine in Thin as suggested in the tread.

Avatar

The preferred method would be to use event delegation using .on() and bind a single click handler to the parent element $('.tasks') and then check the event target to determine the appropriate behavior. This would handle new tasks added after the initial load and would only require a single event binding, rather that one for each task.