RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Way cool Ryan.
How about doing at least a two part series on Twitter Bootstrap? Simple form 2.0 has built-in support for it. It is great for people like me who are programmers but are design challenged :)

Avatar

Great episode -

Why do you have to pass both params[:id] and params[:entry] to the update action in the entries controller?

Avatar

I am working with Spine and I have to tell you: it is amazing the combination Coffee + Spine.

Spine is write in Coffee and it is very simple to understand its source code, but I am a bit afraid about Spine's future, it doesn't seem as active as Backbone.

Underscore is a nice lib, but I think if you are using Coffee, Underscore becomes a kind of useless.

Ryan, talk about Spine too, it deserves.

Avatar

Is it possible to create something like this:

json
{
  message: "...",
  data: [{"array_of_records": "from_db"}],
  meta: "..."
}

Where message and meta - is custom keys (not in AR). And all data goes under data key?

Avatar

Thanks Ryan, Backbone and Rails is indeed an awesome combination.
I was wondering if you could provide the coffee script code in javascript code?

Avatar

Great screencast, Ryan!

Thumbs up for this suggestion! :-)

Avatar

is there a way to delegate all instance variables of @template? for example, @controller, @action and others are not set within the scope of the presenter

I've got a solution but would like to have something better

ruby
def initialize(object, template)
  @object = object
  @template = template
  @template.instance_variables.each do |var|
    instance_variable_set var, @template.instance_variable_get(var)
  end
end
Avatar

Aaron -- I have the same question. Were you able to find a solution to this? I know that the password_reset controller and edit form aren't set properly, but it isn't clear to me how to remedy this to update the identity model with a new password. Any help would be great!

Avatar

Thanks for the episode on RABL.

It would be nice to have JSON output to be formatted in the casts. What about using https://github.com/rfletcher/safari-json-formatter ?

Avatar

Hi Ryan!

I'm trying to build a chat using the private_pub,
I tracking the presence of the users using the Faye events
(http://faye.jcoglan.com/ruby/monitoring.html)
But unfortunately I find out that if I try to publish during the event I'm causing to endless loop so for doing that I should define a client on the server side (http://faye.jcoglan.com/ruby/clients.html) and again unfortunately I discovered that I don't know how to do that using private pub, can you clarify this for me?

here you can find another great explanation to what I want accomplish - http://groups.google.com/group/faye-users/browse_thread/thread/231233016e55aba2?pli=1

10x

Avatar

Hey Ryan,
it's about time! Thnx a lot for this.

I think a comparison off
Knockout, Spine and ember.js would be also very interesting!

There have a much simpler syntax then backbone.js I think AND,
coffee script (spine.js) is fully "supported".

The only thing I ask me all the time, which technology has a future, not all can coexists or for me at least, it doesn't make sense.

What's your opinion on this, have you already checked out other frameworks?

cheers

Avatar

Thanks Ryan. I was wondering if you had any tips for using assets in the JST templates?

Avatar

Awesome job! You have a knack for distilling a complex (or cumbersome) process into simple pieces.

Thanks for pointing out bind has changed. I just upgraded a site and ran into that... and for pointing out there's a third (context) item to pass into a backbone event - never knew that was there - although I'm still not sure what it does. Maybe explain that in part 2!

Avatar

The video is slightly cut-off at the end. Also the backbone-on-rails link in the show notes is pointing to another lib on github.

P.S. Great job as usual. Was meaning to play with backbone.

Avatar

since i have started using backbone, i've gravitated over to Sinatra.

Avatar

Derick Bailey also has a great list of resources with lots of books, blogs and videos.

Avatar

Ditto. Thoughtbot PDF is well worth the coin.

Avatar

I tried this..but seem to be getting a weird error:

Completed 406 Not Acceptable in 9ms...any ideas?

I'm using rails 3.0.5...

Avatar

Is there any book/s about Rails + Backbone best practices?

Avatar

Thank you for making this! I'm a big fan of the RABL project and think they do an excellent job.

Also. Woot first comment.

Avatar

Backbone and Rails is an awesome combination! Glad to see Backbone here on RailsCasts.

Another gem worth reviewing is backbone-rails. (https://github.com/codebrew/backbone-rails)

The scaffold generator of backbone-rails creates ready-to-run routers/models/collections/views/templates.

Avatar

A note to myself, also maybe someone else will find it helpful.

If you wondered how to render resque-web inside your own layout, then use an iFrame with src to resque_server_url. Works like a harm. :)

Avatar

ok, so it was a problem with my routes file. I've now copied the routes from cast #250 (thanks to Tim Fletcher) and everything works fine - without the respond_to code.

Avatar

I am using devise and I am getting this error. I know it has something to do with devise or authentication tokens. Can anyone give me some advise? Here is my error.
I have put this tag in my layout
<%= csrf_meta_tag %>
Completed 401 Unauthorized

Avatar

How do you reference a boolean field?
If the value of a boolean field is false then the field will not be included in the json response

Avatar

I had a problem with the users_controller as posted in the screencast. I copied it directly into my app, and on running got - undefined method `model_name' for NilClass:Class. I'm not sure if this was an issue with my routes, but rails needed a respond_to to be able to handle the "new" request. ie

ruby
def new
    @user = User.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
end

Probably only an issue if new to rails!
(Currently running rails 3.1.3)

Avatar

You have mistyped:

bash
curl http://localhost:9292/faye -d 'message={[channel]("/messages/new",) [data]("hello"}')

Must be:

bash
curl http://localhost:9292/faye -d 'message={[channel]("/messages/new"), [data]("hello")}'
Avatar

one question here:

on the minute 5.11 you show your development log, wich is very clean. did you clean it up by hand?

why i ask? because since rails 3.1 (i think) i get this annoying ouput on every request:

WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

as far as i search, this is a bug on webrick that i couldn't find a workaround. i also test with Thin, but there's a bunch of other information that i don't want either. i just want a clean log as you show it!

Avatar

Oki my bad i found the solution in my Gemfile.

i forgot to add the new environment "staging" in the group where i declare all the gems i was using.

group :development, :test, :staging, :production do
gem 'will_paginate', '3.0'
gem 'devise', '1.5.3'
gem 'cancan', '1.6.7'
end

I hope it will help

Avatar

When i had a new environment staging, i have some issues with my dependencies.

im using rails 3.2.1, with bundler 1.0

when i running
$> rails c staging

i m getting this error

/home/regis/RubymineProjects/bb-rails3/config/initializers/devise.rb:3:in `': uninitialized constant Devise (NameError)

But with
$> rails c development (no issue)
$> rails c production (no issue)

I found a trick, if i put in my application.rb
require 'devise'
require 'cancan'
require 'rails_config'

All the errors disappear why im doing wrong ?

Avatar

something like validates :password, :presence => false
is not possible: rails issue

Avatar

Anyone know if there's a way to remove the :id version of the url from working? For example, if someone visits /users/:username it brings them to that user's profile. If they visit /users/1 it brings them to the user with :id of 1. Is there a way to prevent this?

Thanks!

Avatar

I decided against the Endless Page feature, and instead slightly adapted this RailsCast's code to provide a "Show more results" link, which would append results in much the same manner, but instead by click rather than reached-bottom-of-page detection.

The main change is the addition of the prepend line in products.js.coffee:

products.js.coffee
jQuery ->
  if $('.pagination').length
    $('#append_and_paginate').prepend('<a id="append_more_results" href="javascript:void(0);">Show more products</a>');
    $('#append_more_results').click ->
      url = $('.pagination .next_page').attr('href')
      if url
        $('.pagination').text('Fetching more products...')
        $.getScript(url)

Regarding the view, I only added a surrounding div:

products/index.html.erb
<div id="append_and_paginate">
  <%= will_paginate @products %>
</div>

And in products/index.js.erb I just changed the remove line to $('#append_and_paginate').remove(); so that both the "Show more results" link and pagination is removed when no more results are available.

Of course some css had to be added to make things look nice.

But anyway, the cool thing about this approach is that the "Show more products" link is only added by Javascript (hence only if Javascript is present and enabled). The click handler is bound to the DOM element with id="append_more_results", which is the id of the link tag that is prepended.

Avatar

Nvm... I should have been reading better!

Avatar

Hi guys!

My problem is that I tried everything from your comments to make emails being sended but nothing works.
WebBrick says that email was sent, so what can be wrong?

Avatar

I have a respond_to block in the index action of my controller, so I had to add format.js to render the RJS template associated with this action (i.e. /app/views/products/index.js.erb):

products_controller.rb
  def index
    @products = Product.order("name").page(params[:page]).per_page(10)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
      format.js
    end
  end
Avatar

History option is enabled for my model and when I update the slug, friendly_id_slugs slug is not getting updated. so when I create new record with the same title it is throwing uniquness error.. Please help me on this...

Avatar

is there any way to use this inside a form of active admin?

thanks!! :)

Avatar

In the episode description:
With HTTP response headers you can the cache in the user's browser and proxies. Etags, Last-Modified, Cache-Control and Rack::Cache are all covered here

Should there be "used"?

Avatar

Maybe you could compare/contrast between Pusher and PubNub, as long as I'm dreaming :-)

PS., also no affiliation with either of the above, just interested in using them.

Avatar

A revised version of this episode would come in handy!

Avatar

Thanks @Swart, I was getting auth/failure?message=invalid_response message. The RailsCast code was updated but not the ASCIICast version, so I was using the incorrect "user_info" key.

Avatar

Has anyone used, this in nested form?
I am trying to use this using semantic_form_for , with a single has_many and belongs_to association

Avatar

It would be great to see how to test controllers using warden. I mean it would always be great to see more tests, but authentication systems all have their own implications when testing controllers.

Avatar

It sure does Dan. Awesome. I got it working now.