1

我想让这两个动作用一个javascript模板publication.js.erb响应,而不是为每个动作使用file.js.erb。

##AJAX CALLBACKS##


def publish
    @myth = Myth.find(params[:id])
    if @myth.update_attributes(:status=>2)
      #change status to publish
      @msg  = "Your Myth has been Published"
      @type = "alert-success"
    else
      @msg  = "Your Myth has not been Published"
      @type = "error-success"
    end

    respond_to do |format|
      format.html { redirect_to root_path }
      format.js
    end
  end

  def unpublish
    @myth = Myth.find(params[:id])
    if @myth.update_attributes(:status=>0)
      #change status to publish
      @msg  = "Your Myth has been Unpublished"
      @type = "alert-success"
    else
      @msg  = "Your Myth has not been Unpublished"
      @type = "error-success"
    end

    respond_to do |format|
      format.html { redirect_to root_path }
      format.js
    end
  end
4

1 回答 1

3

在您的每个respond_to块中,只需执行以下操作:

format.js { render 'publication' }

这将使两个动作呈现相同的模板文件。

于 2013-04-01T20:11:39.080 回答