RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Im also slightly dubious about using cookies, if the user plays with their cookies (especially the signed ones), while they are still logged in the announcement code seems to go into a tailspin.

Has anyone else seen this issue ?

Avatar

isn´t there a typo in

[200, {"Content-Type" => "appication/json"}, [terms.to_json]]

shouldn´t it be "application/json"

and how come it works anyway?

Avatar

In order to formulate the proper cache key for some template so that we can check Memcached, does that not require 1+ DB calls (more for nested templates that use some db objects) to fetch the updated_at value on every request?

If so, doesn't this/these mandatory call(s) somewhat offset the performance benefits a bit?

If not, is it implied that cache_digests gem creates static HTML files specific for every call and somehow tag them w/ specific ETags (though this method yields no benefit for users viewing the page the first time)?

Avatar

this looks awesome. how about an episode with Grape and OAuth2? +100

Avatar

And what is wrong with that?

If the business logic is related to that model class and only that model class, then it should go there.

A fat model is not a bad model, so long as your model isn't fat because it's including functionality that belongs to another class.

Avatar

I've revived the MetricFu project if you'd like to try the newest version and submit any issues there. Under active development.

Avatar

SSH Timeouts - avoid ssh disconnection with Capistrano:

1) Add file config/recipes/ssh.rb

ruby
load "config/recipes/base"

namespace :ssh do
  desc "Setup long timeouts for ssh connection"
  task :install, roles: :app do
    sshd = <<-BASHRC
  ClientAliveInterval 60
  ClientAliveCountMax 1200
    BASHRC
    cmd = %Q{grep -R "ClientAliveInterval" /etc/ssh/sshd_config}
    content = capture( %Q{bash -c '#{cmd}' || echo "false"}).strip
    if content == 'false'
      put sshd, '/tmp/sshd'
      run 'cat /etc/ssh/sshd_config /tmp/sshd > /tmp/sshd.tmp'
      run 'rm /tmp/sshd'
      run "#{sudo} mv /tmp/sshd.tmp /etc/ssh/sshd_config"
    else
      run 'echo "FILE /etc/ssh/sshd_config" IS ALREADY UPDATED!'
    end
  end
  after "deploy:install", "ssh:install"
end

2) Edit config/deploy.rb file and add:

ruby
load "config/recipes/ssh"

It works when you call:

terminal
cap deploy:install

or

terminal
cap ssh:install

NOTE 1 - you can call cap ssh:install many times with no worries that file /etc/ssh/sshd_config includes string ClientAliveInterval 60 many times.
It's safe because following code:

ruby
    cmd = %Q{grep -R "ClientAliveInterval" /etc/ssh/sshd_config}
    content = capture( %Q{bash -c '#{cmd}' || echo "false"}).strip
    if content == 'false'
      # Here we know that we must do setup
    else
      # Here we know that setup is already done
    end

NOTE 2 - Additionally I've made the same kind of protection in file config/recipes/rbenv.erb. So now I can call cap deploy:install many times (well, it's not recommended but sometimes it's usefull if you tests deploying process many times).

Avatar

Hi,
as the error messages are hardcoded in ActiveMerchant, there's no elegant way to translate them.

Avatar

Hi,
The routes seetings affects the assets path in production.
Ain't working as is.

Avatar

When I run rackup private_pub.ru -s thin -E production on ubuntu with rails 3.1.3, I get this error:

Terminal
>> Thin web server (v1.3.1 codename Triple Espresso)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
/home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/eventmachine-0.12.10/lib/eventmachine.rb:572:in `start_tcp_server': no acceptor (RuntimeError)
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/eventmachine-0.12.10/lib/eventmachine.rb:572:in `start_server'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/thin-1.3.1/lib/thin/backends/tcp_server.rb:16:in `connect'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/thin-1.3.1/lib/thin/backends/base.rb:53:in `block in start'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/thin-1.3.1/lib/thin/server.rb:159:in `start'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/rack-1.3.6/lib/rack/handler/thin.rb:13:in `run'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/rack-1.3.6/lib/rack/server.rb:265:in `start'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/rack-1.3.6/lib/rack/server.rb:137:in `start'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/gems/rack-1.3.6/bin/rackup:4:in `<top (required)>'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/bin/rackup:19:in `load'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/bin/rackup:19:in `<main>'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `eval'
    from /home/jatin/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `<main>'

How can I get rid of that error?

Avatar

is there a way to insert images on the "non dependency way" to export xls with html in the file?

my images dont work fine, i think i can't do this by this way.

Avatar

that's a bug with Ruby 1.9.3-p362. Using 1.9.2 gets rid of that error.

Avatar

I think you define products as following:

@products = Product.all
@products.class #---> Array

but when you fetch data by calling order():

@products = Product.order(:name)
@products.class #---> ActiveRecord::Relation
in this case you do not have to call Product twice, like Product.to_csv.

Avatar

How come when running the example code, I got the error can't convert nil into String. I am I already added the aws_access_key and aws_secret_key and bucket as well

Avatar

Hi! Whenever I hit the "send" button in the app, the thin server shuts down down due to 'segmentation fault'. Also the browser console gives this error:

Failed to load resource http://localhost:9292/faye.js

So, why is not working? I'll really appreciate someone's help. Thanks!

Avatar

What a great episode! I really like that you use Redis. I'd be grateful to see more episodes about Redis and Rails.

Avatar

I've been looking for a tutorial like this everywhere. I'm having an issue though. Whenever I go into my url and put /products/pricing (for example) it loads, but the url drops 'products' so its just /pricing. I followed the example code line by line, and then copied and pasted the code from the site into my project. Any thoughts?

Avatar

Really nice article Ryan. I remember I in the asp.net world the concept of services is really strong. Beyond nicer readability Services are considered a fundamental component of an application for handling business logic.

In Ruby achieving great code readability is often harder due to developers having multiple possible ways of separating responsibility based on taste. But separating business logic isn't exactly emphasised as well as it could be which leads often hard to read code. Although mostly a nice feature I sometimes see the flexibility offered by Ruby language as its curse too.

Avatar

Typo in rack middleware?

appication/json

Avatar

Anyone have the problem of it occasionally not cropping the image after the upload?

Occasionally one slips by.

Avatar

This was what I needed to do to fix my issue. Thanks.

Avatar

Hey guys check out this gist https://gist.github.com/4421177 here at icalialabs have been using this for a while, enjoy!

Avatar

Hello, anybody could tell me a good way to bypass the tenant scope? I have tried calling unscoped and perhaps I'm not doing it right but it seems not to do anything. All the queries are made with where tenant_id=....

Thanks!

Avatar

The application works only on safari, I guess

Avatar

This is absolutely great. Thanks.

Avatar

I was getting a wrong argument error (1 for 0) on <%= calendar ....%> and upgrading from rails 3.1.8 to rails 3.2 solved that issue.

Avatar

Agreed - this seems like a big issue per my comment above, and I'm really curious as to how an experienced Rails dev would tackle it.

Avatar

I've also had many frustrating experiences with encoding issues in user generated files. You might want to look at https://github.com/chrisgogreen/roo which allows CSV options, or my fork which incorporates CSV options and implements filthy brute force encoding to UTF-8: https://github.com/aew/roo.

Speaking of which - I've only ever been able to overcome pc/mac encoding issues in user generated files using iconv. String#encode or force_encoding don't seem to do it. I must be missing something as I've run into this repeatedly on different projects and have yet to figure out the "rails way" of dealing with it. I guess I'll keep staring at iconv deprecation warnings for the foreseeable future.

Agree with other comments on large files - roo / active record will be slow and have a large memory footprint which can get you in trouble using delayed_job on Heroku.

Avatar

I know it's kinda late, but +1 for testing external web services as well.

Avatar

Just before the episode was release I started working on a gem to solve this very problem. It's called CanBe. I've also implemented the code from this episode to use my gem here.

Avatar

I want to implement this in MongoDB database. Any idea? If some hints then welcome. Also i asked this question in stackoverflow.
http://stackoverflow.com/questions/14090621/sortable-table-columns-is-not-working-working-in-mongodb

Avatar

Just a head's up, not sure if it's a change, however unsubmitted Open Graph action types (i.e. review) can only be tested with an app Administrator or Developers account. Test accounts won't work. I was getting this error:

Koala::Facebook::ServerError: type: OAuthException, code: 2, message: An unexpected error has occurred. Please retry your request later. [HTTP 500]
Avatar

That works great - does this also have built in support for Helper tests?

Avatar

Never mind, I found the issue and fixed it.

Avatar

Encoding::ConverterNotFoundError in Pages#index

Showing /usr/local/rvm/gems/ruby-1.9.2-p320@table/gems/exception_notification-3.0.0/lib/exception_notifier/views/exception_notifier/exception_notification.text.erb where line #22 raised:

code converter not found (UTF-8 to UTF-16)

Avatar

I first implemented this episode and it work. However, I am using simple_for in my project, so I switched from 'form_for' o 'simple_form_for' in the devise new registrations view.

When the user log in using Twitter, for example, they are redirected to the new registration form as eexpected, since I my project requires email and Twitter does not provide user email address.

The project is that the registration form is not pre-populated with the first and last name on first pass. However, when you lick on the 'log in with Twitter' link again, then the correct behaviour (pre-populating the first name and last name fields) happens.

My question is, how do I avoid having to click the link twice in order to get the correct behaviour?

Thnx.

Avatar

As long as you're calling a method directly on the object being iterated over (ie. Page#save) this will work.

Avatar

This works for transactions (tons faster than :truncation):

ruby
DatabaseCleaner.strategy = :transaction
class MiniTest::Spec
  before :each do
    DatabaseCleaner.start
  end
  after :each do
    DatabaseCleaner.clean
  end
end
Avatar

I'm not sure if this is the best practice. But I moved the services tests from 'test/services' to 'test/unit/services' to make them part of 'rake test:units'. This works good enough for me.

Avatar

I had to run unicorn as the web server with multiple workers, I couldn't get pow to serve the pdf even if I changed the number of workers pow was using.

Avatar

I'm using haml and had to change the call to calendar to be of the display format (I had to do = calendar do |date| instead of - calendar do |date|). Is this expected? Ryan had the call to calendar marked as nondisplaying code, but had it the other way initially.

Avatar

I have never seen it done this way before so threw me as well. But one thing still gets me why in the other class methods you are not using the attribute @user ie,

ruby
  def send_email
    generate_token
    @user.password_reset_sent_at = Time.zone.now
    @user.save!
    UserMailer.password_reset(@user).deliver
  end

  def expired?
    @user.password_reset_sent_at < 2.hours.ago
  end

Is it because of the attr_reader?

Avatar

Did figure it out? I am having similar problems although I am working in Linux. Tested on multiple browsers and I don't think this has anything to do with them. I seem to have cookies set to permanent despite of the fact that I never checked the remember me box. Please help if you have any idea what this can be caused by. Thank you.

Avatar

If you execute your test library with the rake command the tests in the service directory are automatically included if you are using RSpec, but not if you are using Test::Unit. Is this right?

If yes, what is the best way to auto include the tests in the service directory if you are using Test::Unit?