RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I already had the gem selector for guard in my Gemfile. But I got around it a different way. I just created the Guardfile before running guard init rspec:

shell
touch Guardfile
Avatar

Testing of external web services would be awesome!

Avatar

It's also nice for customers who prefer PayPal.

Avatar

But VCR doesn't allow me to record 2 API calls in one function. Any idea how to handle this?

VCR is specifically designed to allow you to put as many HTTP interactions into one cassette as you want. What problem are you having?

I'd encourage you to send an email describing your issue to the mailing list, where I generally answer VCR questions.

Avatar

Very helpful screencast!

I think "render nothing: true" is a typo?

Avatar

is it possible to decoreate current_user ?

Avatar

yes, but when you want to keep the lazy loading behaviour? doing that will hit the database immediately (well, has_many association is more problematic than belongs_to) what about that?

Avatar

Maybe I would centrally put this method in ApplicationDecorator (not tested):

ruby
def element_if_present(key, attribute)
  if self.send(attribute)
    content_tag(:dt, I18n.t(key)) + content_tag(:dd, self.send(attribute))
  end
end

Then you have to call @user.element_if_present('your_scope.twitter', 'twitter') instead and also have the presenter output this container markup but only once in ApplicationDecorator.

Avatar

I'm having a challenge with VCR and I hope someone can help me with that.

I have a planning API I wanted to connect to and i want to validate the data from the api with my models.

In my system an Employee has an association with with an address. To get a valid employee i need to do two requests to the API.
1. To get the employee
2. To get the address of the employee.

But VCR doesn't allow me to record 2 API calls in one function. Any idea how to handle this?

Avatar

Your Github link does not work.

Avatar

My features are running twice too. Is that normal? I understand that two spork environments are requires, but surely my features should only run once?

Avatar

Great tutorial! How do I validate an email from a specific domain before creating the new user? I tried :

validates_presence_of :email, :if => :in_co?
validates_uniqueness_of :email, :if => :in_co?

def in_co?
email == 'test@test.com'
end

but it didn't do anything.

Thanks in advance!

Avatar

Great episode!

+1 For cancellation and insufficient funds episodes.

Avatar

I also had the issues with classify not recognizing singular names like "business", so I went with

ruby
  private
  def find_commentable
    commentable = nil
    params.each do |name, value|
      if name =~ /(.+)_id$/
        commentable = name.humanize.constantize.find(value)
      end
    end
    commentable
  end
Avatar

Or $('input[type=submit]').disable() to get the same behavior and 'disabled' css style class added.

Avatar

This is a fantastic tutorial on writing degradable ajax.

Avatar

Sounds like a Rails 3.1 issue with the asset pipeline and threading: https://github.com/jdpace/PDFKit/issues/110

Avatar

I find myself getting the following error unless I include guard in my Gemfile too:

No such file or directory - Guardfile (Errno::ENOENT)

FWIW, using rails 3.1.1 and guard-rspec 0.5.0

Just saying incase anyone else runs into this problem too

Avatar

But when you get out of it in production you'd better have a well-tested billing system :-)

Avatar

Exactly and Ryan's pronunciation is ... ahem ... pas tr

Avatar

SWEET!!!!!!!

Ill use it in my next project which starts tomorrow. Thanks for sharing.

Avatar

I will definitively look into Ranked Model, looks really great! Thanks Jean!

Avatar

Hey rbritom,

since Savon v0.9.7, the response object comes with a #doc method which returns a (memoized) Nokogiri object:

ruby
zipcode = response.doc.css("USzip").inner_text
Avatar

Can i use vcr with capybara + selenium?

Awesome episode. I add capybara-mechanize for OAuth testing in my app.

Avatar

Savon means soap in French in case you want to know.

Avatar

I was using VCR and come into a particular problem, i do no communicate to the web service directly i first establish a reverse ssh tunnel to another computer in the network, so i could not figure a way to capture the request. Any suggestions?

Avatar

Oh, one other thing, it is also posible and very easy to read the values with nokogiri using a css selector.

doc = Nokogiri::XML(response.to_xml)
zipcode = doc.css("USzip").inner_text

I find it most useful when dealing with collections

doc.css('ProductCode').each do |product|
    ...blah
end
Avatar

Rails 3.1.1, 3.1.0: problem with routing. No route matches:

Started GET "/uhoh/" for 127.0.0.1 at 2011-10-17 16:46:27 +0400

ActionController::RoutingError (No route matches [GET] "/uhoh"):
  

Rendered /home/r_wilco/.rvm/gems/ruby-1.9.2-p180@rails-3.1.0/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
Avatar

there are 3 types of errors, one from the SOAP service it self, one from savon and one from the communications gem ( timeout error or no response) for the first one the web service should be able to notify of the error somehow (return a status tag) so you can handle that without the need of a begin rescue block the other cases are handled with with beging rescue.

Avatar

Awesome as alway.
Ryan i will take this opportunity to motivate a topic that i think you have work with and i did have to solve when dealing with savon. The web service i comunicate with takes a couple of second to respond ( the rails app waits and does not answer to user request), so i took an approach i saw you using in episode 243, i used beanstalk and stalker, but then came the problem of updating the view instantly without the need to refresh the page when stalker/beanstalk finish.

Apparently i was the only one who didnt know how it was done but i still could not find a good example. Then in episode 281 i saw you were using faye with stalker.

I did figure out how to update the view with faye from stalker, but, could you touch this topic in a future episode? i think is a very common problem and there is not much documentation out there about how to update the view asynchronously after a background process finishes working.

Avatar

Good cast for me.
What about errors handling raised by Savon?
Only by request.succes? ?

Regards
Sssebaaa

Avatar

Thanks Ryan..
Nice to see this as a screencast.
We once wrote a small app that consume complex types objects using Savon.
Please see https://github.com/spritle/awssoap4r

We wrote a small blog as well http://www.spritle.com/blogs/2011/09/28/consume-soap-webservices-using-ruby-with-savon/

Thought this might help your followers/readers
Regards
Balaji, Spritle Software

Avatar

I would love to see this episode continued to include the user_activation module and how to setup a "resend activation email" feature for times when a user doesn't get the email.

Avatar

Tying to pick up some Coffeescripting from this episode:

javascript
  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)
  
  handleStripeResponse: (status, response) ->
    if status == 200
      $('#subscription_stripe_card_token').val(response.id)
      $('#new_subscription')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

Should the subscription.handleStripeResesponse function call not contain the two arguments (status and response) in the code above? The definition of the function includes these two.

Please explain.

Thanks.
Bharat

Avatar

I love that you snuck in "When will TextMate 2 be released?"

Avatar

Great episode. But "Website Payments Pro" is not available in Germany :(

Avatar

Thanks man! you saved me hours of searching....

Avatar

is it possible to add a 'can' ability to a whole namespace?

Avatar

What about accepts_nested_attributes?

Avatar

Problem with logout function and current_user value.

When calls logout, only resets session (reset_session) but not resets current_user to nil.

So when using logged_in? function after logout, it returns true because logged_in? tests current_user value.

Avatar

What if instead of displaying "None Give" for missing fields you wanted to not render the field. That would mean something like

users/show.html.erb
<dt>Twitter:</dt>
<dd><%= @user.twitter %></dd>

would have to become

users/show.html.erb
<% if @user.twitter %>
    <dt>Twitter:</dt>
    <dd><%= @user.twitter %></dd>
<% end %>

or just

users/show.html.erb
<%= @user.twitter %>

and have the presenter output the container markup, which I don't really like. Wondering if there was any cleaner way to do that? Thanks!

Avatar

Any chances to subscribe Railscasts Pro via Paypal yet?

Avatar

Agreed with Mischa! We need a Paypal Recurring billing II episode. =D Keep the great work Ryan!

Avatar

I am a little late to the party, but I would recommend looking at ranked model, it handles ordering a lot nicer than the (old) acts_as_list.

Here's a snippet from the Github project page:

This libarary is written using ARel from the ground-up. This leaves the code much cleaner than many implementations. ranked-model is also optimized to write to the database as little as possible: ranks are stored as a number between -8388607 and 8388607 (the MEDIUMINT range in MySQL). When an item is given a new position, it assigns itself a rank number between two neighbors. This allows several movements of items before no digits are available between two neighbors. When this occurs, ranked-model will try to shift other records out of the way. If items can't be easily shifted anymore, it will rebalance the distribution of rank numbers across all memebers of the ranked group.

Avatar

A simple Procfile for chatter-after is:

web:    bundle exec rails server -p $PORT
faye: bundle exec rackup faye.ru -s thin -E production

Then run with "foreman start", and the main app is at http://localhost:5000

Avatar

with rails3

instead of InstructorRegistration.where(:joins => :user, :order => 'users.last_name')

I think you want

InstructorRegistration.joins(:user).order('users.last_name').paginate

Avatar

I had the same question. But I think, I will have to show full list if want the users to sort through complete list.

Avatar

I noticed this one before.. just want to point out that you shoud use $ as argument, not global variable

jQuery ($) ->
  ...