RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Upon login I get the following error returned:

NoMethodError in SessionsController#create

undefined method `authenticate' for #Class:0xb69d4638

ruby /user.rb
  class User < ActiveRecord::Base
  has_secure_password

  attr_accessible :email, :password_digest, :password, :password_confirmation

  validates_uniqueness_of :email
end

Any suggestions?

Avatar

Ryan, I am suprised those tests passed. You should use should have_no_content instead of should_not have_content, see Capybara README. Maybe PhantomJS makes all AJAX synchronous? I don't know. Thanks for tons of great episodes!

Avatar

Anybody having trouble getting the data attribute working on rails 3.0.1?

Avatar

Any advice for converting an existing app. Should the database be destroyed and recreated? Does the tenant model and an instance need to be created first? Any tutorials or examples online? Thank you.

Avatar

Yep, in this case I'd have done it exactly like you. In some cases you want to plug in variables server side. For those cases rails templates are useful.

Avatar

He might simply have not known about it. ;-) I didn't either, so thanx for the tip.

Avatar

Devise works fine for me with this multi-tenancy solution with users scoped inside the tenant (user tables are inside the tenant schema, not in public) and without storing the subdomain in the user model. Well, almost works fine...

The only problem i hit was when editing registrations, because the Devise::RegistrationsController does not set the schema search path. That's easily solved by creating your own:

ruby
class RegistrationsController < Devise::RegistrationsController
  prepend_around_filter :scope_current_organization
end
ruby
devise_for :users, :controllers => { :registrations => "registrations" }
Avatar

You didn't waste any hour. You sacrificed those hours for the betterment of humanity.

Avatar

Hi. Thanks for another awesome podcast. I have a task to solve. I need different configurations in "/config/recipes/templates/nginx_unicorn.rb" per host. How can I use different variables for e.g. server_name?

Avatar

You probably need an /etc/init.d wrapper for sidekiq. Whenever I want to monitor anything with monit, i write a simple wrapper for it. You can find them around pretty easily, plenty available for unicorn and other gems that require bundle exec functionality.

Avatar

Yeah, I also had issues getting the form fields to show up if I didn't put anything in the controller.

My user model had a profile model. The relationship was has_one and belongs_to.

I had to add the following to the users controller.

Users Controller

ruby
def new
  profile = @user.build_profile
end
Avatar

Is there any cast on using database or memcached to store session data instead of cookie.

Avatar

Nominating this article for an update too.

I'm interested in understanding the best practice for view/partial specific javascript.
I could use a yield statement at the top of my view/partial to fill in a javascript_include_tag in application.html.erb, but this leads to multiple HTTP requests to serve assets in production. Also, you need to do some hacks for it to work well with asset pre-compilation.

I'd appreciate it if someone could point me in the right direction. Even more so if someone could convince Ryan to do an updated screencast on this one!

Avatar

Thanks Ryan. One thing I hope to see in the future is avoiding the use of that kind of templates hide.js.erb I like pretty much using directly the client side

js
$(document).on('ajax:success', '.announcement', function(){
  $(this).remove();
});

I would like to know how other people do this.

Avatar

Did you ever figure out how to use the hooks? I'm trying to figure it out, too.

It appears you need to use a "Custom Job", but I am unable to get the custom job to execute. I see the call to enqueue, I see the record inserted into delayed_jobs table. Then I see the job being locked, and the job being deleted from the table. No other hooks are called, nor is my process.

Avatar

Thanks for this link to DHH's presentation. The russian doll caching strategy is really the first I have seen (Rails or otherwise) that doesn't have a big long list of tradeoffs -- they figured out how to make it just work. It's worth backing up to watch all of DHH's stuff. Much of what he talks about is how to get all the benefits of über-responsive client-side performance without completely chucking Rails and moving to Backbone.JS

There are at least three whole topics are performance and caching for Ryan to cover. Memoization as covered here is a tiny little aspect of caching in comparison.

(Update: I am behind on my RailsCasts -- Ryan has done a lot of them already. Go Ryan!)

Avatar

So Im a bit outside the rails community as a network admin. I was a developer primarily years ago, but mostly PHP/Python/C/shell scripts (and before that dBase and Foxpro :).

So simple question - what's the benefit over just doign a chroot into a debootstrap'd image?

Avatar

Hi all!

Nice screencast but I don't like:
1. Using lambda in the routes.rb file
2. Problem with adding subdomain: false in every routing that I want to clean urls from subdomain
3. Problem with changing every link_to with _path to _url also for cleaning urls (many hours of work in large project).

My solutions:
ad. 1. Instead of using the lambda I can use smart regular expression:

ruby
match '/' => 'blogs#show', constraints: { subdomain: /^(?!www$)(.+)$/i }

Regular expression /^(?!www$)(.+)$/i doesn't allow to handle main domain with www. Really nice, isn't?

ad. 2. I prefer to add following line in development.rb and production.rb files:

ruby
config.action_controller.default_url_options = { subdomain: false }

Now using subdomain is disabled by default. I don't need to add subdomain: false in many lines in routes.rb file... ufff.

ad. 3. I prefer to create file url_helper.rb with a few lines of code:

ruby
module UrlHelper
  def url_for(options = nil)
    if request.subdomain.present? 
    and request.subdomain.downcase != 'www'
    and !options.nil? and options.is_a?(Hash)
    and options.has_key? :only_path
    and options[:only_path]
      options[:only_path] = false
      puts "Changed options[:only_path] to true. options=#{options.inspect}" unless options.nil?
    end
    super
  end
end

Now I do not have to change thousands of links :-) ufff :-))

It works for me. Do you like it?

P.S. I'm using Rails 3.2

Avatar

agree... would love to see a change password, remember me as well

Avatar

There is need to revise this video, we now have rails g devise:views for rails 3

Avatar

Thanks for letting us know, I've corrected the ASCII version

Avatar

Ryan -- you briefly mentioned the default format of datepicker was mm/dd/yyyy, and changed to something ruby can parse, like yyyy-mm-dd. I wanted the date format displayed as an American date in forms (and elsewhere).

I am sure I have an inelegant solution, so any recommendations are welcome.

To handle date parsing, I used the https://github.com/jeremyevans/ruby-american_date gem, which make Date.parse work as it did in ruby 1.9.2, accepting the mm/dd/yyyy format.

For the form field, I needed to handle both edit and create:

ruby
  <div class="field">
    <%= f.label :begins_on, "Start Date" %>
    <%= f.text_field :begins_on, :value => date_formatted(@my_model.begins_on || Time.now), :data => {:behavior => 'datepicker'} %>
  </div>

The date_formatted method is a helper in application_controller.rb like this

ruby
  helper_method :date_formatted
  def date_formatted(datetime)
    return nil if datetime.blank?
    datetime.strftime("%m/%d/%Y")
  end

and the JS is done (definitely inelegantly) in an application-wide file

CoffeeScript
$ ->
  `$(document).on("focus", "[data-behavior~='datepicker']", function(e){
      $(this).datepicker({"format": "mm/dd/yyyy", "weekStart": 1, "autoclose": true});
  });`

Hope this is helpful for someone with similar requirements.

-- Tom

Avatar

To add to my original comment above, and to reiterate what a couple of other posters have suggested, I would love a follow-up tutorial to this showing how to deploy a new (separate) Rails app on the same server.

The reason I ask is that, given that I am a lone developer (and, probably like most folks here, my apps are generally quite small), my app only takes up around 5-10% of the allocated Linode so, when it comes round to deploying a second app (which will probably be just as tiny), it would appear to be more sensible to deploy it within the spare space rather than purchase an extra Linode.

There appear to be a few solutions above, but they are a bit daunting for a noob (particularly when one's current app just "works" and you're wary of breaking anything!), so a short screencast would be just dandy.

On a related note - Linode offer a backup service for around $5 a month (for the basic server option), so I'd recommend getting this if you're a noob like me. I think nightly backups are taken.

Avatar

Hi -- I think the issue you're facing is that since ruby 1.9.3 Date.parse doesn't handle the ambiguity between dd/mm/yyyy and mm/dd/yyyy.

I dealt with this using a gem called american_date -- since your format is non-american, you may want to use the gem code (short, sweet) as an example.

Avatar

Perhaps model changes are logistically too far away from the templates, where as a controller knows intimately how it needs the views prepared.

Avatar

Hi, thank you for this episode. I am using this authentication for a small Rails App and wanted to login from a iPhone App but i am not really sure which URL i have to call within my iOS App. It would be nice if someone could "push" me in the right direction.

Regards,
Sebastian

Avatar

im using spork and simple cov, and simple cov does not seem to cover everything. In my Model it says "No data available"

Any solutions to this :)

Avatar

There's a gem that can help with one-off notifications, too: Paul Revere by Thoughtbot

Avatar

There is an error in the ASCII version

def load_commentable
resource, id = request.path.split('/')[1,2]
@commentable = resource.singularize.classify.contantize.find(1)
end

should be:

def load_commentable
resource, id = request.path.split('/')[1,2]
@commentable = resource.singularize.classify.constantize.find(id)
end

change contantize to constantize and put id between parenthesis.

Avatar

is this causing webrick to become unresponsive for anyone else?

Avatar

Any idea how to have it so that the controller is not being seen in the url?

For example, i got it working with this:
https://www.domain.com/users/jimmy-hendricks

I like it to be this instead:
https://www.domain.com/jimmy-hendricks

Just like facebook/twitter did in theirs.
Thanks

Avatar

What about using a different column than "slug", I see some reference to setting configuration options in the documentation, but I have no idea how.

Avatar

I found the testing really distracting on this one. I just wanted to see how you handle the announcements and it made the episode much more verbose than usual.

I think showing how to do testing is outside the scope of each episode and could be tackled once if needed.

Avatar

Steve - did you try clicking in the text box? My datepicker doesn't appear until I do that.

Avatar

I wonder why you always seem to use Time.zone.now, Ryan. Time.current achieves the same thing in Rails: It uses Time.zone.now if Time.zone is set and otherwise falls back to Time.now. See http://apidock.com/rails/Time/current/class.

Avatar

I´m getting an Dead::Client error with phantomjs. I think it´s an issue when using fontawesome webfonts. Has anyone a solution to that?

https://github.com/jonleighton/poltergeist/issues/44

https://gist.github.com/3996516

https://gist.github.com/4003597

Avatar

I had a problem using fragment caching when calling the partial in response to javascript i.e. in index.js.erb and using <%= j render @collection %>

I was getting a response, but the javascript to append the response to the page just didn't fire

Avatar

I'm fairly new to rails and everytime I google to get some info, railscasts keeps popping op. So far I've been really happy I've subscribed :)

Avatar

I've made some short testing and capybara-webkit is still around 20% faster in our use case. I suggest you try it for yourself and see, that's a matter of minutes or hours if run longer tests to have better metrics.

Avatar

cache_digests and fragment caching is 1 piece of the puzzle.
Rendering out templates is actually pretty heavy on the server as well. Using this method will get you closer (performance-wise) to using a full client side JS solution, which takes all the fun out of using Rails.

If your controller actions are heavy then considering caching the underlying data as well.
In various places in my app I'm using:
1. Just fragment cachine
2. Fragment caching + action caching
3. Model caching + fragment caching

I'm not too fond of action caching, but it does speed things up tremendously if you can use it.

Avatar

This is specifically for fragment caching.
JSON would need to get cached by directly calling Rails.cache.fetch in your model or controller.

Avatar

What is the reason that it sends empty e-mails ??

Avatar

Thanks Ryan for this really cool episode. I'm currently working on a side project and I'm committed to full TDD so I'm not allowing myself to add any new code without a failing test first.

The only thing that in my opinion could be improved regarding your episode is the way that the hidden_announcement_ids are passed to the scope; in my opinion, it is not very intuitive. I would probably use a hash with the key {skip: []} or something, but that's just me.

Thanks again!