如何在控制器中将render .erb添加到render / redirect_to json?

时间:2019-05-26 12:36:00

标签: ruby-on-rails ruby

我要在销毁操作(#comment-counter-fiels)之后添加render 'destroy'的更新。

我的观点_fields.html.slim

span#comment-counter-field.comment-counter.color-brand-fourth.size-small
      = user.comments_count || 0

控制器comments_controller.rb中的操作:

def destroy
    @comment.destroy
    respond_to do |format|
      format.html {
        if @comment.obj.comments.blank?
          redirect_to forum_root_path
        else
          redirect_back fallback_location: root_path
        end
      }
      format.js {   
        if @comment.obj.comments.blank?
          render json: { redirect_to: forum_root_path } # Here
          return
        else
          render json: { status: 'no_content' } # Here
          return
        end
      }
    end    
  end

视图_comment.html.slim中控制面板中的操作:

            - if can?(:destroy, comment)
              li
                = link_to 'DELETE IT',
                  comment_path(comment),
                  method: :delete,
                  remote: true,
                  data: { destroy_remote: true, destroy_target: '.comment', type: :json },
                  class: 'js-comment-destroy'

视图destroy.erb

var commentsCount = "<%= User.find(@comment.user.id).comments_count %>"
$('#comment-counter-field').text(commentsCount)

我使用了很多情况,但不幸的是没有用...可能是更改json请求,但idk是如何实现的。

0 个答案:

没有答案