Rails: Render a status code without rendering a template
Asked Answered
O

1

6

I'm trying to make an ajax call to a rails (5.1.3) app. I want the targeted action in the controller to render a status code 200 and do nothing else. Here's how the action looks like.

  def some_delete_action
    respond_to do |format|
      format.js do
        render status: 200, layout: false
      end
    end
  end

The issue is that rails tries to load the template associated with that action. Since there's no such template in my app, I get a ActionView::MissingTemplate error.

How can I render the status code without having rails try to reach for the template ?

Orang answered 3/11, 2017 at 10:37 Comment(0)
F
21

You could use the ActionController::Head and pass :ok as value:

# home_controller.rb
def do_nothing
  head :ok
end

# routes
get 'home/do_nothing

# request in view
<script type="text/javascript">
  $.get('<%= home_do_nothing_path %>')
</script>
Felicitasfelicitate answered 3/11, 2017 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.