【问题标题】:Should I still respond to html if view is rendered with javascript?如果视图是用 javascript 呈现的,我还应该响应 html 吗?
【发布时间】:2016-09-22 03:04:59
【问题描述】:

我有一个与 react 集成的 Rails 应用程序,我的视图是用 react 呈现的,包括 html (JSX)。我注意到当我的视图是常规的 erb 视图时,我收到了一些 format.html 的回复,现在它们不是,我是否仍应回复 html 以防万一(即使我看不到用户如何使用我的应用程序(如果他们的 javascript 已禁用)?

例子:

 def destroy
    @comment.destroy
    respond_to do |format|  
      format.json { head :no_content }
      format.html { redirect_to @question, notice: 'Comment was deleted.' }
    end
  end

我可以摆脱 html 响应吗?

【问题讨论】:

    标签: ruby-on-rails json ajax reactjs


    【解决方案1】:

    是否保留是个人选择。我有时会这样做,但更少的 LOC 会使代码更简洁。要删除它,您有多种选择。您可以将respond_to 保留原样,然后删除 html,例如:

    def destroy
      @comment.destroy
      respond_to do |format|  
        format.json { head :no_content }
      end
    end
    

    但您也可以通过以下方式从每个操作(甚至更少的 LOC)中删除 respond_to

    # put this LOC at the top of your controller, outside of any action
    respond_with :json
    
    # then each action is much simpler... you just assume it's always json
    def destroy
      @comment.destroy
      head :no_content
    end
    

    【讨论】:

    • 太好了,所以这是个人选择,我有时会对这些简单的选择感到偏执,并从中大赚一笔。谢谢塔林
    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多