??yy 2016-08-28 10:12 采纳率: 0%
浏览 29

Rails Ajax请求响应

I am using Rails CoffeeScript to call an action in my controller, which is fine, but I can not get the response to work. I have a form with a list of budget lines. I want to allow the use to add a new line using CoffeeScript so I don't need to reload the question. I have got the following in CoffeeScript:

  $("button[id^='new_budget_line']").on 'click', (event) ->
    category_id = $(this).attr('name')
    child_economy_id = $('#child_economy_id').val()
    $('#form_id').submit ->
      valuesToSubmit = $(this).serialize()
      $.ajax
        type: 'POST'
        url: $(this).attr('action')
        data: valuesToSubmit
        dataType: 'JSON'
    $.ajax({
      type: 'GET'
      url: '../child_economy_lines/create_line'
      data:
        child_economy_id: child_economy_id
        category_id: category_id
        cost_type: 'direct'
      dataType: JSON
    }).done (response) ->
      alert "Hey"
      $('#test_append').html("Hey you now!!")

And the following in my controller

  def create_line
    logger.debug "Hejsa fra create line - category id #{params[:category_id]}"
    @child_economy_line = @child_economy.child_economy_lines.build(:child_economy_category_id => params[:category_id], :cost_type => params[:cost_type])
    if @child_economy_line.save
      respond_to do |format|
        format.js {render nothing: true}
        format.json {render :json => "test"}
      end
    end
  end

The action in the controller i called fine, and the new line is created, but I can not the actions after the ajax call to work. The part from .done ...

Can anybody help me identify where it is going wrong?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码