【问题标题】:Rails: respond_with RESTful controller template after non-RESTful actionRails:非 RESTful 操作后的 respond_with RESTful 控制器模板
【发布时间】:2012-02-26 18:21:42
【问题描述】:

尝试向其他 RESTful 控制器添加“发布”操作时出现“模板丢失 - 缺少模板选票/提交”错误。显然它正在寻找一个不存在(也不应该存在)的submission.html.haml 视图。

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

我想在这两种情况下都用这个控制器中的“显示”动作来响应。但不确定应该是什么语法。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 respond-to respond-with


    【解决方案1】:

    我认为您可以在其中使用redirect_to 来指定路径:

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

    (将ballots_path 替换为您的路线。)

    【讨论】:

      猜你喜欢
      • 2016-05-05
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多