简体   繁体   中英

Cucumber Testing js.erb functions when no html.erb exists

My rails application is using js.erb views instead of html.erb views for specific windows. This works fine in practice, but when I'm using cucumber tests with capybara it gives me an error of

Missing template admin/groups with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml],    :formats=>[:html], :locale=>[:en, :en]} in view paths

When I click the button pertaining to this particular view. There is no groups.html.erb but there is a groups.js.erb. I want to somehow tell cucumber/capybara to not try to render the groups.html.erb but still render the groups.js.erb. I would prefer not to generate an unnecessary html file to render the same thing the escape javascript below is doing.

groups.js.erb:

$("#admin_content").html("<%= escape_javascript(render :partial => 'groups') %>");

Relevant admin controller method:

def groups
    @groups = Group.all
end

You should use proper MIME type handling for your responses. Try this:

def groups
  @groups = Group.all
  respond_to do |format|
    format.js
  end
end

eventually you can be more specific about how to respond :

format.js { render layout: false }

more info on respond_to here .

note: if your controller only manages js responses, you can add respond_to :js at the top of the class, and then use respond_with @object in your actions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM