RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I haven't been able to use VCR in tests with :js => true (capybara + selenium)

The first time I run the test it passes and the cassette is recorded.

However, when I use the cassette, the test fails with Unregistered request:

POST http://127.0.0.1:7055/hub/session/..... with body '{"url":"http://subdomain.myapp.com/settings"}'

The url is different every time.

I'm using nginx locally so I can use ssl, *.myapp.com is going to 127.0.0.1:3000, I've set Capybara server_port to on 3000 and Capybara.app_host = "subdomain.myapp.com". Is that what's messing things up?

Avatar

We used this to communicate with Yodlee API. Worked wonderfully!

Avatar

Can you give any example, how to use em-net-http with Savon?
Regards

Avatar

Can i use vcr with capybara + selenium?

To add to what Ryan said...it depends on what you mean. If you want to use VCR to record/replay HTTP requests made by your rack application, and you are using capybara + selenium to do full-stack integration testing--then yes. VCR isn't coupled to a particular capybara driver (or any testing library, really) so it should work fine, as long as you are making HTTP requests with one of the supported HTTP client libraries.

If you are wanting to use capybara + selenium to test-drive an external website like what Ryan demonstrated with capybara-mechanize...then I'm not sure. I wasn't aware capybara-mechanize allowed you to do that.

Basically, VCR will work with anything as long as you are using one of the supported HTTP client libraries (or a library built on top of one of those--like how rest-client is built on top of Net::HTTP), you have control of the ruby process where the HTTP requests are being made, and you have a way to wrap the code making the HTTP requests in a VCR.use_cassette call.

Avatar

Instead of letter_opener you can also use http://mocksmtpapp.com/ it's simply brilliant to test emails in development.

Avatar

While on SOAP and webservice, the rails app that I work on is expected to expose couple of methods as SOAP Webservice. I know Ruby has ditched SOAP in favor of REST, but I really dont have an option as the client app expect it. Is there any gems out there that support creation of a SOAP web service on Rails 3.x?
Thanks!

Avatar

but the appointment will be of class Appointment not AppointmentDecorator, or?

Avatar

Thanks for this, it also works perfect in Rails 3.1

Avatar

The previous code works for new models, but you can get the existing id like this:

 <li id="entry_<%= f.object.id || f.object_name.gsub(/[^0-9]+/,'') %>">
Avatar

Heh. I'm glad you still have the source on github for pro. That hande_none was driving me bonkers...

Avatar

+1 on handling insufficiant funds, etc

Avatar

Hi,

I am using cancan 1.6.7 on a Rails 2.2.3 app.

My ability class is as follows:

ruby
class Ability
  include CanCan::Ability
  
  def initialize(user)
    can :read, :all
  end
end

When loading a page, I am getting the following error:
NameError in AController#edit
uninitialized constant CanCan::Ability::I18n

Any one knows why?

Thanks.

Avatar

Good questions.

1) The save_with_subscription method is doing a valid? check and therefore does not raise an exception when validation fails. I am doing save! inside the valid? check because if the data somehow got invalid between then and now I want to know about it.

2) Handling cases where save fails is tricky because the transaction has already been placed. I suppose you could do an automatic void but then there's always the possibility of that not going through. I think this is better to be handled manually. If save fails then it will raise an exception and hopefully there is something setup to notify you when an exception occurs so you can handle the situation manually (such as voiding the transaction).

Avatar

Thanks for posting this, I had forgotten about the prop method.

Avatar

I wish there was a good solution for this but I can't think of any alternative recurring payment method that will work in Germany. If you have suggestions please let me know.

Avatar

Yes, I will be adding a PayPal checkout option soon, stay tuned.

Avatar

I will be adding a PayPal checkout option soon, stay tuned.

Avatar

I have not yet made the Pro features open source, but i plan to at some point. I just want to make sure it's air tight before I do because I'm dealing with more sensitive information.

Avatar

PayPal support will be coming soon. Thanks for your interest in RailsCasts Pro.

Avatar

To do this you can place any sensitive data config files in the shared directory on the server and then symlink/copy them over upon each Capistrano deploy. See the how I'm doing it here for an example.

Avatar

I don't plan to switch to Stripe since I already have Braintree setup. They're both great though.

Avatar

I'm using Braintree at the moment, but will be adding PayPal as an alternative checkout option soon.

Avatar

This is using the new hash syntax present in Ruby 1.9.2. If you are using Ruby 1.8.7 you would need to convert this to the old hash syntaxrender :nothing => true.

Avatar

Sounds like a fun idea for a future episode, thanks for the suggestion.

Avatar

Glad you like the revised episodes. The 3.1 tag is for episodes where the main topic is a feature of 3.1 and cannot be applied to 3.0. It is true one would need to translate the CoffeeScript into JavaScript, but other than this the functionality is the same.

Avatar

Pagination is tricky because I'm not certain how you would sort across pages. You may want to do simple up/down arrow buttons that increment/decrement the position instead of drag + drop sorting.

Avatar

Thanks for the suggestion. Pushing API requests into a background process is a good thing and would make a good episode. Good idea.

Avatar

Thanks for the suggestion. I plan to cover TTD more in future pro episodes.

Avatar

I don't think so, but I haven't tried it. VCR needs to stub out the HTTP request, so that needs to happen in Ruby. I don't think the Capybara Selenium driver uses Ruby at all to perform the HTTP requests. It instead communicates with Selenium at a higher level. I believe the same goes for Capybara Webkit and other alternatives.

That said, what you can do is setup a proxy Rack server which all remote requests go through. This can forward all HTTP requests to their remote destination using Net::HTTP and therefore use VCR to record it. It would look like this: Capybara > Selenium > Rack proxy server > Remote server.

Avatar

Since VCR only stubs HTTP requests it won't work over an SSH tunnel. However what you can do is setup a proxy server that is before the SSH tunnel and have VCR record requests that go to that server. I haven't actually tried this, and it's a bit advanced, but hopefully it will give you some ideas.

Avatar

The delivery_method isn't a method that you define. It is normally set to :smtp or :sendmail depending on what you want to use to send email. See the guide for further details.

Avatar

Coda, I found this link:
http://railsforum.com/viewtopic.php?id=40499

so your >> here? <<

could be something like

class => "net_device_<%= f.object_name.gsub(/[^0-9]+/,'') %>"

not sure about the content tag and how you specify the class/id for CSS.

I'm using a fieldset as follows:

 <fieldset id="entry_<%= f.object_name.gsub(/[^0-9]+/,'') %>">

Good luck :)

Avatar

airbrake also has a (experimental) javascript exception notifier, that logs javascript exceptions too. the stack trace is often not too useful, but it's worth a look if your application relies heavily on javascript.

the free plan is good enough to test it out and setup is easy

what bugs me a little bit about it is, that I can't log ruby and javascript exceptions to different projects, maybe in the future if I have a little time I can make a fork of the gem that will do just that

Avatar

Thanks Jakub, i was stuck and you came through for me.

Avatar

When you set the action mailer delivery method in your production config file, where should that method live?

Avatar

thank you! could you make a PRO screencast with basics of TTD?

Avatar

It's worth to mention that now this is the default behaviour, as Rails offers query cache, so you don't have to store the result of an expensive query on an instance variable. If your application performs the same query on the same request more than once, Rails automatically returns the result of the first execution, without touching the database twice for the same query.

Additionaly, if you enable identity map, you have the extra benefit of always getting the same object representing a record of your database, instead of creating new objects.

Avatar

You're sure, but to sort by id or created_at retrieves the same record on this case. And it's better to sort by an integer column than by a date column, for performance reasons.

Avatar

Definitely. Or just "scope" as it is now.

Avatar

Man, this is just begging to be revised! :)

Avatar

Hi ryan,

First: thanks so much for this cast, it was great and really informative!

Second: say I want to group my orders by week instead of per day. I am not very savvy with how this works in a helper method, could you give an example?

Avatar

I used it in our project. Cool library!!!

Btw, take a look on em-net-http (https://github.com/jfairbairn/em-net-http). Sometimes SOAP server might respond slowly. I used this library with Savon to improve performance and I reached it.

Avatar

I wanted only Users with the admin attribute set to true to access resque-web.

Since we're using Devise, the current user is accessible like this:

ruby
request.env['warden'].user

So I made the below off the back of the example here:
http://www.scottw.com/securing-resque-server-rails-3

ruby
require 'resque/server'

class SecureResqueServer < Resque::Server
  before do
    redirect '/' unless request.env['warden'].user.admin
  end
end

If we were using Devise's "roles" (where each role has it's own table) we could simply do this in the routes file:

ruby
authenticate(:admin) do
  mount Resque::Server, :at => "/resque"
end
Avatar

I went through this tutorial on a new app with Rails 3.1.1 and it worked perfectly. However, I am now trying to use this on an existing app using Rails 3.0.9 with ActionMailer and DEVISE, and it does not work. I am pretty confident DEVISE uses ActionMailer based on the settings, but whenever I send an email to another use, they get the email and not me.

Has anyone been able to get this to work with DEVISE?

Avatar

Hi guys!

I'm having a little problem trying to understand 'content_tag_for', could you please give me a hint?

I'm not pretty sure how to get/use the IDs from inside a partial :( here is my code:

form.html.erb
<ul id="net1" class="connectedSortable">
  <%= f.simple_fields_for :net_devices do |netdev| %>
  <%= render 'net_device_fields', :f => netdev %>
  <% end %>
</ul>
_net_device_fields.html.erb
<%= content_tag_for :li, >>here?<< do %>
  <span class="handle"><%= image_tag("icons/grippy.png", :alt => "Drag and Drop! :)") %></span>
  <%= f.text_field :dev_name, :placeholder => "name", :size => 7, :maxlength => 7 %>
  <%= f.text_field :mac_address, :placeholder => "mac address", :size => 16, :maxlength => 16 %>
  <%= link_to_remove_fields "remove", f %>
<% end %>

Thanks in advance! :D

Avatar

I think you have to eagerload all associations needed by your view in you controller:

ruby
@user = UserDecorator.new(User.includes(:appointment).find(params[:id]))

Then there should be no extra query when calling user_decorator.appointment.

Avatar

Hi,

There is a mistake in the asciicast here:

gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ &crarr; 
    /darwin/i

Text encoding trouble probably :)

Avatar

I've been doing:

ruby
presenter.h.should_receive(:render).with('blah/_blah', :arg => 'value')

I like your way better, although I wish this was possible:

ruby
presenter.method.should render_template('blah/_blah').with_locals(:arg => 'value')