Search code examples
ruby-on-railscontrollerbefore-filter

Why is a before_action not executed when the action is called from respond_to?


I have an action, which sets an instance veriable before :edit and :new actions in a controller. This works if I call these actions directly in a request. However, if I call :update or :create and they fail, after which within a respond_to block format.html { render action: :edit } is executed, the before_action does not get triggered. Why is that?


Solution

  • Because render action: :edit doesn't actually call controller action, but only renders edit template.

    You'll find more info in Rails guides:

    http://guides.rubyonrails.org/layouts_and_rendering.html#the-difference-between-render-and-redirect-to