【问题标题】:Must respond_to :html be specified when only :js is expected当只需要 :js 时,必须指定 respond_to :html
【发布时间】:2015-08-25 01:25:57
【问题描述】:

使用 Rails 4.2.3。通常在一个动作中,我希望进行 Ajax 调用,并用 JS 来响应(使用 JS 来更新某些字段),而不是期望它响应 HTML。示例:

def create
  @post = Post.new(params[:post])
  if @post.save
    respond_to do |format|
      format.html
      format.js
    end
  end
end

format.html 还需要吗?我通常只使用以下代码:

def create
  @post = Post.new(params[:post])
  @post.save
end

并创建一个create.js 以呈现它。到目前为止没有问题,但这是一个好习惯吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    是的.. 我一直这样做.. 如果您只返回一种给定的类型,那么为什么还要打扰响应块。这只是额外的样板,在这种特殊情况下没有任何用途

    记住爱因斯坦的话:“让事情尽可能简单,但不要简单。” :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-23
      • 2017-07-06
      • 2019-09-21
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2013-09-22
      相关资源
      最近更新 更多