Search code examples
ruby-on-railsruby-on-rails-3controllerruby-on-rails-3.2respond-with

How to change the location in a respond_with without overriding the behaviour when a model is not saved


Given the following controller.

class Posts_controller < ApplicationController
    def create
        @post = Post.new(:params[:post])
        flash[:notice] = 'Post successfully saved' if @post.save
        respond_with(@post, :location => my_custom_url)
    end
end

In the event that a post is not saved, i.e. because it fails one or more validations, the controller goes to my_custom_url rather than rendering the 'new' view which it would do when no location would be given.

Is there any way to stop this behaviour from being overriden?


Solution

  • It turns out that the location is not changed after all, but that the code fails during url generation https://github.com/rails/rails/issues/2798.