RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

sounds like you are handling 2 different use cases:
* data publishing and subscription
* keep-alive ping-pong

I suggest do not use private_pub to keep track if user is opening the website in the browser. instead, go deep into faye and use the ping feature.
have a look at:
https://github.com/faye/faye-websocket-ruby

Avatar

ok so all the problems come from if the user authenticates via oauth but there is no one logged in.

suppose you didn't active confirmable, a malicious user could have registered an account with your email, you can't assume if the email is the same it is the same person. basically you need the user to authenticate as a user in devise as well as authenticate via oauth to link them

ryan did get it right where he creates a user with the same email if the user doesn't exist.

the problem is if a user account already exists with the email and its not logged in.

so in that case, user.save will fail, but then do you redirect to registrations or sign in?

i would think redirecting to sign in would be better since you don't want the user to be trying to create an account with the same email as he once did before.

Avatar

I just spent some time and made it. not sure if you have already figured it out or not.

After you deployed your Ruby web app (Rails, Sinatra, ...) to heroku, you should setup another heroku apps for Faye. Either you use the same Ruby web app source code or use Faye-Heroku-Cedar.
to start the Faye correctly on heroku, the key point is about the Procfile.

what Procfil does in this case is just running the "rakeup -R private_pub.ru" command to start the service.

checkout Faye-Heroku-Cedar and heroku document about Procfile

Avatar

just a supplement to Steven. I had problem with that as well.

If you use Firebug, you should see an javascript error in the console saying PubSub not found or undefined (something like that).

It is because in the layout twitter-bootstrap-rails put the <%= javascript_include_tag "application" %> at the bottom.
which is after the <%= subscribe_to .... %> is being called.

so, by moving that <%= javascript_include_tag "application" %> back to the top. the private_pub should work well with bootstrap-rails generated code

Avatar

I wouldn't mind seeing a Revised Railscast on this episode, that included passing arguments to rake tasks.

Avatar

Was using Chrome 19.0.1061.1 (Official Build 125213) dev when this was happening. Everything works fine on Safari. This is all on a Mac BTW...

Avatar

These 2 screencasts was really amazing Ryan.
Thank you very much for that stuff.

I always love your clarity (sometimes it's still very complicated though, even if you try to make it as simple as possible ;))

What I don't like with backbone is that you can't generate your forms based on your backend models (as simple_form does it for you)...

Any Idea ?

Avatar

I am seeing the same behavior when running thin. Did you ever figure it out?

rails (3.2.2)
backbone-on-rails (0.9.1.0)

Avatar

Thanks. This was a really nice and useful screencast for me. I also wrote a related blog post covering Pjaxifying search form, pagination, etc. http://blog.assimov.net/post/19250353925/rails-search-form-with-pjax

Avatar

I'm curious about the testing aspect too, especially the save_with_payment method in the Subscription model.

Anyone have an RSpec example for this or a resource for the underlying concept?

Avatar

there is a case the logic doesn't handle correctly in my opinion

suppose you create a user
bob@aol.com

now you log out

ok now you sign in with facebook
it will not find a provider , and not find current_user, which then
will create a new User and try to save it, however that new user will have the same email as the provider, so it will consider duplicate and not allow the user to save

Avatar

If you're having problems getting Rails to save your date...

Several people have noted that Rails will silently to update the column is the date is in the wrong format, e.g. MM/DD/YYYY. Here's a little CoffeeScript function that you can use to extend the jQuery .datepicker() method:

CoffeeScript
datePicker = (selector) ->
  $real = $(selector)
  $display = $real.clone().attr({class: null, id: null, name: null})
  $real.hide()
  $display.insertBefore($real)
  $display.datepicker({altField: $real, altFormat: 'yy-mm-dd'})

The function just creates a new input, using the original as a template, hiding it, but still using as the altField that Rails will read on the PUT request, using a Rails-lovin' date format.

Call the function from my_model.js.coffee like so

CoffeeScript
$ ->
  datePicker("#model_datefieldname")
Avatar

figured it out. it was problems with indentation, which meant that the code which was supposed to be in init() was actually outside the method, and was being called before the router was loaded. small gotcha for someone not used for coffeescript.

Avatar

Hi guys, I have an app listing a bunch of inventory items and on my views I am showing columns with item.created_at and item.updated_at. After following this tutorial I have all of my columns sorting fine with the "arrows", except that I can't get the item.created_at and item.updated_at columns to sort. Any tips?

Thanks for any help!!!

Avatar

Hey Great screencast Ryan! I have a question, in the view, there is some syntax I don't understand. What is JST[]? Is that using the JST library? I'm trying to understand how backbone knows where and how to load the external template files. Any insight you could give on this would be VERY helpful.

Thanks!

Avatar

Agreed. I hear so much about Chef and want to use it, but can't find any good resources.

Avatar

Take a look at what rbenv doesn't do in comparison to RVM in the Readme: https://github.com/sstephenson/rbenv

rbenv is trying to be more, specific, as to what it does and not interfere with other existing software. (Apparently RVM rewrites functions like 'cd').

Avatar

What does your sprite folder structure look like?

Avatar

nice tutorial!

but i can't initialize the router. i get an error telling me that "Bb.Routers.Entries is not a contructor". (my app is called Bb instead of Raffler.) i suspect this is related to load order, but get find a way to fix it.

file bb.js.coffee:

window.Bb =
  Models: {}
  Collections: {}
  Views: {}
  Routers: {}
  init: ->
        new Bb.Routers.Entries()     <--- this is the offending line
        Backbone.history.start()

$(document).ready ->
  Bb.init()

file entries_router.js.cofee:

class Bb.Routers.Entries extends Backbone.Router
        routes:
                '': 'index'
        
        index: ->
                alert 'homepage'

file application.js:

//= require jquery
//= require jquery_ujs
//= require underscore
//= require backbone
//= require bb
//= require_tree ../templates
//= require_tree ./models
//= require_tree ./collections
//= require_tree ./views
//= require_tree ./routers
//= require_tree .

gem versions:
backbone-on-rails (0.9.1.0)
rails (3.2.1)
coffee-rails (3.2.2)

Avatar

@Ryan: excellent RailsCast, as always!

the following article from RABL's author Nathan Esquenazi is very interesting, and may be a good addition to the references section:

If you're using to_json, you're doing it wrong

Avatar

After this and the Pro episode I switched over to sass-rails and everything is generally good, except that this: rails g bootstrap:themed products -f

No longer works. You can do it all manually, but it would be nice to use the generator. Anyone have any suggestions?

Avatar

Not sure if anyone else will have this problem, but I first added twitter-bootstrap-rails, then tried out boostrap-sass, then tried to go back and there is some kind of gnarly dependency error that chokes on 'therubyracer'. After a lot of Googling, it looks to be unresolved and I'm seeing show-stopper suggestions like "switch your development platform" :/

Anyway, it could very well be my environment, but be warned if you want to just "test out" bootstrap-sass, you might have a hard time getting back.

In any case, switching back to bootstrap-sass was no problem (once you delete bootstrap.js.coffee, per @kikouli). I was very torn on which way to go and this made my mind up for me, so maybe it worked out better in the end!

Avatar

Thanks a lot, this helped me too. I was going to delete that, but RubyMine was barking at me telling me "require bootstrap" needed that file to be there. Works fine now.

Avatar

Yeah, after seeing DHH's example I had some fun spending a few minutes making a little "plugin" for that[1].
We already use the as option[2] in our code though so I haven't had the chance to really work on that yet.

[1] = https://gist.github.com/2014589
[2] = http://guides.rubyonrails.org/security.html#countermeasures

Avatar

it works after removing app/assets/javascripts/bootstrap.js.coffee, per kikouli's comment about popover.

Avatar

I think you mean Twitter Bootstrap. And you are corrent, it does provide several mixins to use and variables to manipulate. :-)

Avatar

Great screencast! I've also been using Compass before and would love to see a screencast about this soon.

For those who have asked: Compass is quite a lot more extensive than Bourbon and offers more functionality (such as sticky footers and image spriting). However, I think Bourbon is much easier to get started with and use, and for those projects that doesn't need the largeness of Compass, I believe Bourbon might just be the right thing to use.

Also check out Twitter Bootstrap, which Ryan covered on a recent episode. While Bootstrap gives you a lot more than just mixins and variables for your SCSS files, they provide you with a number of these as well.

Avatar

Thanks very much, that seems to have done it!

Avatar

See my answer to Matthew above. You're better off not using Sprockets to gather your SCSS files.

Avatar

Make sure you are not using Sprockets in your application stylesheet file. As Ryan mentions at the beginning of this episode, Sprockets will compile each of the SCSS files individually, and you will therefore not have access to the bourbon.css.scss in your SCSS files (unless explicitly imported). I see no reason to use Sprockets for your sCSS files – other than the (rather flawed, in my opinion) require-tree functionality, so it's generally better to rename the application.css file to application.css.scss and use SCSS @import instead. Watch the beginning of this screencast once more to see how Ryan does it.

Avatar

Does anyone have any hints of how they could implement this on a has_many to has_many relationship, using a third modal to join them?

Avatar

attr_accessible can also take an option role using the :as option which allows you to define multiple levels of accessors. You can then use the :as option with new, create, create!, update_attributes, and update_attributes! methods to use the different levels of accessors. Alternatively you could use :without_protection => true if you wanted to bypass all of the mass assignment protection for a single statement, e.g., in a seed file.

http://guides.rubyonrails.org/security.html#countermeasures

Avatar

Hey Ryan,

First of all, I really like your stuff- just subscribed today.

Anyway, this is the first time I've been unable to follow your railscast. For whatever reason, after doing as you said and installing devise, all requests for sign up/in/out are rerouted to the index page.

I feel like it has to do with using devise 2.0.4/rails 3.2.1, but I'm not sure. Any chance you could make an updated version of this episode?

Avatar

Is it right way to use view_context in controllers? It's not caching and recreate every call lib/abstract_controller/rendering.rb#L74-76

Avatar

I've only used Compass and doesn't see any difference..
which one is better?

Avatar

Yeah, thanks a lot but:
I can fix it, so RABL would be in handler list, BUT in rails 3.2.2 it seems to me that it is trying to render json while requesting for html. #180

Avatar

Love these casts, as a rails noob I'm killing heaps of hours watching and tinkering :) Thanks Ryan.

I am at my wits end atm though... I've isolated everything my app should use down to:-
Sorcery
Omniauth
CanCan
twitter-bootstrap (converted to sass)
and Simple Forms.

Clean, clear and simple....Not.
Cannot for the life of me integrate (what would seem to be the most simplest of tasks) simple forms with a Sorcery Login without getting errors on the 'remember_me' field.

Simple forms doesn't have a simple_form_tag option which would work best on a login form from the sessions controller new method. Instead I have to create a @user instance in that method, but then get errors on the 'remember_me' field "undefined method `remember_me'"

Any help would be greatly appreciated.

I mean Greatly! Huge thanx in advance :)

Avatar

That's cool, I actually built a javascript only library thats very similar: https://github.com/andrew/split.js

Avatar

Let's say my service stores User records. If a customer signs in with Twitter and creates some records, they will be associated with their new user id. But, later if they sign in with another provider on a different browser, there's no browser session to track, and since the other provider doesn't exist in the database, it will create a separate User record. The customer will not be able to access the records they previously created when they logged in with their Twitter account. It's concerning because I know some users will forget what they used to sign in with originally. Is there a workaround for this?

Avatar

Nice work Andrew!

Slightly different and built for Rails, I've released the split_tester gem. It integrates easily with Google Analytics so that you can use Analytics as your evaluation dashboard.

https://github.com/jhubert/rails-split-tester

So happy to see more people A/B testing their apps.

Avatar

A colleague recommended me the original episode almost five years ago. When Railscasts went pro, I immediately subscribed.

It's my way of saying - I'm not doing it for the new content, but rather to show my appreciation.

And as far as I'm concerned, Ryan may very well not post anything for one year, the existing content alone is well worth the monthly subscription.

Avatar

Thanks Andrew -- looks like a very useful gem.