Search code examples
ruby-on-railsruby-on-rails-3respond-torespond-with

Rails: respond_with RESTful controller template after non-RESTful action


Getting "Template is missing - Missing template ballots/submission" error when trying to add a "publish" action to an otherwise RESTful controller. Clearly it is looking for a submission.html.haml view, which doesn't exist (and shouldn't).

class BallotsController < ApplicationController
  respond_to :html

  def index
  ...

  def publish
    @ballot = Ballot.find(params[:id])
    if @ballot.publishable?
      @ballot.submitted = true
      flash[:notice] = "Ballot successfully submitted" if @ballot.save
    else
      flash[:error] = "Could not submit. Ballot incomplete."
    end 
    respond_with(@ballot, location: ballot_url(@ballot))
  end
end

I'd like to respond with the "show" action in this controller in both cases. But not sure what syntax should be.


Solution

  • I think you can do a redirect_to in there to specify the path:

    respond_with(@ballot) do |format|
      format.html { redirect_to ballots_path }
    end
    

    (Replace ballots_path with your route.)