Question

I have a UsersController that has index and new actions. I use haml, the index haml file contains the following code:

= link_to 'Add User', new_user_path, { :remote => true, 'data-toggle' => 'modal', 
  'data-target' => '#modal-window', 'class' => 'btn btn-primary pull-right' }

So when the user clicks 'Add User' the _new.html.haml partial is presented to the user as a modal. It works great. Here's the new action in the controller:

def new
  @user = User.new

  respond_to do |format|
    format.html
    format.js
  end
end

Now in my controller spec I am trying to do the following:

describe "new action" do
  before do
    get 'new'
  end

  # specs for 'new' action go here
end

However that gives an error

Failure/Error: get 'new'
 ActionView::MissingTemplate:
   Missing template users/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
     * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007faa44b03218>"

Presumably because it can't find new.html.haml which of course doesn't exist because the file is a partial named _new.html.haml. What is the correct syntax for getting the partial? If not, how can I test the new action?

Was it helpful?

Solution

Okay, here's what I changed in the new action to make it work:

respond_to do |format|
  format.html { render :partial => 'new' }
  format.js
end

When the app is running rails figures out to render the partial, but I guess it needs to be explicit for rspec. If there are better ways to do it I'd be glad to hear them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top