Take the 2-minute tour ×

I'm facing a problem with rspec and controllers

i'm spec'ing an update action, so to do it i call the following code:

put :update, :id => "1", :ntp => {:name=>'myservah'}

My controller has the following code:

def update
 if @ntp.update_attributes(params[:ntp])
  flash.now[:notice] = "Successfully updated ntp."
  flash.now[:error] = ((errors_to_a(@ntp)*'.<br />')+'.')
 render :partial => 'update'

And i'm getting the following error:

Failure/Error: put :update, :id => "1", :ntp => {:name=>'myservah'} Missing partial ntps/update with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "#"

My question is: should i explicit say which handler i have? Today my partial is named '_update.js.erb'.

BTW, my code works, i render the "js" version of update, since i just run some scripts

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

Rails detects the format of the response from certain request parameters, including the http accept request header (intelligent guessing), the :format parameter or the page extension.

Unless specified, Rails default the request format to :html.

As you can see, your request defaults to :html which means Rails is trying to render the _update.html.erb template. There are a few changes you can apply to fix the issue:

  1. Always specify the request format in the test, if different than html.

    # emulate a js request
    put :update, :id => "1", :ntp => {:name=>'myservah'}, :format => :js

    If the action is intended to respond to an AJAX request, run the test using the xhr method

    # emulate an XHR request
  2. Return 406 with any non-js response. Change render :partial => 'update' to

    respond_to do |format|
      format.js  { render :partial => 'update' }
      format.any { head 406 }
share|improve this answer
i had to change just the controller and add the format.. thanks for the "406" tip.. btw the hxr syntax, at least in rspec 2.0.0 is wrong. –  VP. Oct 5 '10 at 8:22
Sorry, it's xhr. I fixed the answer. –  Simone Carletti Oct 5 '10 at 9:46
add comment

Your Answer


