【问题标题】:rails update modal fields via ajax on modal index pagerails 通过模态索引页面上的 ajax 更新模态字段
【发布时间】:2018-07-09 15:19:37
【问题描述】:

我正在尝试更新模态索引页面上的模态字段。模态索引页面为每个工单提供两个下拉字段。更改此下拉列表的值后,应更改模式字段。

ajax 调用有问题。除了 ajax 调用,其他都正常。

这是我目前为止的

关于tickets_controller.rb

def edit
end
def update
 respond_to do |format|
  if @ticket.update(update_ticket_params)
    format.html { redirect_to @ticket, notice: 'Ticket was successfully 
    updated.' }
    format.json { render :show, status: :ok, location: @ticket }
  else
    format.html { render :edit }
    format.json { render json: @ticket.errors, status: :unprocessable_entity 
     }
  end
end


private
def update_ticket_params
  params.require(:ticket).permit(:status,:priority)
end

js代码

$(document).on('change',".index-ticket-status", function(){
var ticketid = $(this).attr("ticketid");
var value    = $(this).val();
$.ajax({
        type: "POST",
        url: '/tickets/' + ticketid + '',
        data: {status:value },
        success: function(){
            $(this).hide;
        }
  });

});

控制器编辑视图

<%= form_with(model: ticket ,remote: true)  do |form| %><%= form.select(  :status, ['open', 'pending','resolved','closed','waiting for customer'], {},{:class=>"form-control status-select"}) %><%= form.submit class:'btn btn-block go' %>

【问题讨论】:

    标签: jquery ruby-on-rails ajax modal-dialog


    【解决方案1】:

    没关系,我通过在索引页面上为每张票添加表单来修复它。

    <%= form_for ticket, remote: true,:html => { :id => "form_#{ticket.id}" } do |f| %><%= f.select(  :status, ['open', 'pending','resolved','closed','waiting for customer'], {},{:class => 'index-ticket-status',:id =>"#{ticket.id}-index-status",:ticketid =>"#{ticket.id}"}) %><%= f.submit :style => "display: none;" %><% end %>
    

    在字段更改时提交表单的js代码(提交按钮被隐藏)

    $(document).on('change',".index-ticket-status", function(){
     var ticketid = $(this).attr("ticketid");
     $('#form_' + ticketid).submit();
    });
    

    控制器

      def update
    respond_to do |format|
      if @ticket.update(update_ticket_params)
        format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
        format.json { render :show, status: :ok, location: @ticket }
        format.js 
      else
        format.html { render :edit }
        format.json { render json: @ticket.errors, status: :unprocessable_entity }
    
      end
    end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 2017-05-08
      • 1970-01-01
      • 2016-10-22
      • 2019-02-14
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多