ruby-on-railsruby-on-rails-5ruby-2.5

return no stopping controller action for remote call


I have an issue with a form sendind two parameters to a controller. I would like that the erb.jstemplate not be triggered if the update of the object fails.

I have added return at the end of my action but the view is still triggered.

Her is the bit of my controller which I think is responsible for actionning the view or not :

if @object.update(some_params)

...
  respond_to do |f|
    f.html {redirect_to some_path_here}
    f.js
  end
end

return

Solution

  • That's happens, because according to convention over configuration Rails automatically renders action view, if not explicitly specified.

    In order to avoid rendering, one of the options would be setting head if @object hasn't been successfully updated:

    if @object.update(some_params)
    
    ...
      respond_to do |f|
        f.html {redirect_to some_path_here}
        f.js
      end
    else
      head :unprocessable_entity
    end