??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条回答 默认 最新

    报告相同问题?