我想让两个 Action 用一个 javascript 模板 publication.js.erb 响应,而不是为每个 Action 使用一个 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
最佳答案
在每个 respond_to
block 中,只需执行以下操作:
format.js { render 'publication' }
这将使两个操作呈现相同的模板文件。
关于jquery - 如何让相同的 javascript 代码响应多个 ajax 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750736/