RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Great point. Sometimes you can get by with making the status panel more generic such as "My Account" so it can work for any situation. However that isn't always wanted.

Alternatively you can use JavaScript to load the dynamic content. See Episode 169: Dynamic Page Caching on how to do this. I plan to revise that episode in the near future.

Avatar

Yes it is but RABL offers you JSON and XML.

Personally, I preferred RABL. I guess its up to everyone.

Avatar

Ignore me. Got it sorted now. Added gem 'bcrypt-ruby', :require => 'bcrypt' to my gemfile and it works perfectly :)

Avatar

Can't you just use an ordered list <ol>?

Avatar

This is what I do and it works quite well.

Avatar

Thanks for that link murdoch. Useful little Rack example.

Avatar

I'm getting the same problem. gem 'bcrypt-ruby' is in my gemfile and I've run bundle install. Still no joy.

I'm on OSX and running Ruby 1.9.2 (via Rbenv) and Rails 3.2.0.

Any ideas?

Avatar

Is jBuilder similar to RABL? (I think it is...) If so - what are the pros/cons of jBuilder vs RABL?

Avatar

Check acts_as_api, really useful and modular. You'll define templates once for json and xml.

Avatar

Well, one way is caching the whole page and loading user specific info like the logged in as box via ajax.

Avatar

I was wondering the same thing. Right now I use partial caching with memcache which has helped a lot.

Avatar

So, I've yet to see a good description of how to make caching play nicely with a user login status panel. You know, the little "logged in as John Smith" div that every Devise-based Rails app seems to have.

Obviously, you don't want this to be cached publicly, but then you lose all of that wonderful public cache goodness with Rack::Cache. I saw the conditional content bit at the end for public caching, but I'm not sure exactly how that would work -- does that mean that anyone who hits this page simply wouldn't see those elements? If so, how do you render flash notices or login status?

This -- the fact that there's usually just one or two parts of a complicated page that I DON'T want to cache publicly -- is what stops me from using public caching in Rails. I'd love to hear comments; it's quite possible that I am simply misunderstanding how it works. Maybe some more quality time with curl would help me...

Avatar

Thanks Ryan, very easy to understand. Do you plan on making an episode on Backbone.js soon?

Avatar

Hi,

my issue is not really related to this railscast but I'm also getting :3000:3000 when using _url helpers.

What you did could solve this issue? So yes, could you explain, I don't find this 'with_subdomain' function anywhere.

Thanks.

Avatar

I have used ActiveModel::Serializers::JSON module to make a serializer/presenter class and I like this approach better than Jbuilder.

Avatar

There is XML Builder that comes bundled with Rails ("builder" gem)

Avatar

Is there any gem for generating xml representation in such kind of way?

Avatar

Is it possible to do fragment caching with Jbuilder?

Avatar

Thanks for reporting that! The ASCIIcast has been corrected.

Avatar

is your partial in a different view directory than the one it's called from?

Avatar

Why in god's name do we need to use global variables? How is this an acceptable practice?

I'd love to be convinced that this is a good way to develop.

Avatar

Hi, I am fairly new to both rails and heroku. Based on ur suggestion, I researched quite a bit on how to create rake task and call Mailman::Application.run, but was not able to find any good examples. I'd very much appreciate if you can share some sample code around this. Thanks!

Avatar

OK...the "problem" was I have to restart the server and the constant "appears" :)

Avatar

Maybe I'm stupid or maybe I'm just a newbie (this for sure), I've tried to put the costant in the environment.rb file and use it from the application but I get an "uninitialized constant" error, I understand YAML files are a better place to put configuration but why I get this error? (I'm using Ruby 1.9.2 and Rails 3.1.3, probably something changed since the screencast)

Thank you very much and sorry for the noise ;)

Avatar

What about separating into partials and then:

ruby
<%= render "myview_#{Rails.env}" %>

Then have the following partials:

myview_demo
myview_production

etc...

Avatar

Check out this fork of TableBuilder if you're still looking for something like this. It's been gemified so it's simple to use with bundler and won't result in any deprecation warnings with Rails 3.2

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

Hi, just wanted thank you for a great episode, and point out that the ASCII cast states that I should use view_content rather than view_context. Took me a while to figure that out =)

Thanks.

Avatar

It've been confused as well, that the sprocket directive //= require_tree . in app/assets/javascripts/application.js does not include javascripts in /vendor/assets/javascripts. But your solution works well. Same goes for stylesheets, put this line in your app/assets/stylesheets/application.css:

css
 *= require vendor

and create a file /vendor/assets/stylesheets/application.css containing:

css
 *= require_tree . 
Avatar

Is it just me or can we not access the new key/value store attribute in a rake task? I'm trying to set values of some keys in a kv store attribute, and I'm getting an unknown attribute error.

However, I have no such issue when I access or try to set the same attributes via the console.

I also tried to set values in the class initialize method, which works fine in Rails and the console. I get the same error when the class in initialized in a rake task.

Any ideas?

Avatar

Great screencast! Is it possible to add count of subelements to json/xml output? e.g. photo has many comments, and I want to add comments_count to /photos.json ?

Avatar

In ASCII cast there is a '=' missing between self.row_colors AND ["DDDDDD","FFFFFF"]

BTW
Great Cast!!!

Avatar

can anybody please help me. how to configure mailman on production.
should i need to keep my production terminal allways open after typing script/mailman_server.rb

Avatar

Really great. I customize it to use the new html5 and have an autocompletion on association without javascript.

ruby
class SimpleFormBuilder < ActionView::Helpers::FormBuilder
#...
  def text_field_with_list(name, *args)
    options = args.extract_options!
    options[:list] ||= options[:label].downcase.pluralize
    t = content_tag :datalist, options[:datalist].collect{ |option| content_tag(:option, option) }.join.html_safe, :id => options[:list] if options[:datalist].is_a? Enumerable
    text_field(name, options) + t
  end

  def objectify_options(options)
    super.except(:label, :datalist)
  end
end
ruby
<%=f.text_field_with_list :pseudo_auteur, :label => "Auteur", :datalist => Auteur.pluck(:pseudo)%>
Avatar

For me the application does not show the near by locations, even if I run the code as is..

Anyone had similar problems?

Would appreciate any help on how to fix this.

Thx

Avatar

Hi Ryan,

Great screencast as usual.

I would also recommend you check out Pusher. It is a commercial service, but provides a lot of great value out of the box (and very easy to get started with for free).

It would also probably make another great screencast.

-Scott

P.S. I have no affiliation with Pusher....I am just a big fan. :)

Avatar

Also interested in this, hope Ryan can share some light on this question and the one above ;)

Avatar

Is it possible to use cancan or other authorisation methods with elasticsearch or tire?

Avatar

Thanx 'ed'..Your code for dynamic date fields really helped me...I've lost my 1 week over this....Thank you..!!

Avatar

If we add a few middle on the top of the stack and gain performance for those items, will it slow down the rest of the app because the middleware stack is now deeper?

Avatar

How can I push only the messages sent between the people in a one-to-one chat? Should I create a channel for every pair of users?

Avatar

Nevermind... just figured out that I was passing in user instead of user.id.

Avatar

Is there anyway for me to use both friendly_id and the record id? I'd like to use friendly_id in my consumer-facing URLs, but the record id in my admin URLs... is there anyway to do that?

EDIT: Ok, I see that I can manually change the parameter in the URL, and it still works as expected.. but my path helpers all use the friendly id, e.g.:

ruby
edit_admin_user_path(user.id) #=> /users/some-friendly-id/edit

How can I get this?

ruby
edit_admin_user_path(user.id) #=> /users/4/edit
edit_user_path(user.id) #=> /users/some-friendly-id/edit
Avatar

That was really helpful, thanks!

Avatar

fixed it by using

@message = Message.new
@message.parent_id=params[:parent_id]

instead of

@message = Message.new(:parent_id => params[:parent_id])