必承其重 | 欲带皇冠 2016-05-30 13:39 采纳率: 0%
浏览 18

Ajax请求不起作用rails 4

I am trying to hit the database at the exit of a field without reloading the form using Ajax. The controller must do some process on the data obtained from the database and respond to the view. The user will then continue with the input.

I am missing something and just can't see what is wrong. I have the same piece of code working in a Rails 3.2 application without a problem. In Rails 3.2 the request goes to the check_matricula method, but in Rails 4, it goes to the show action.

The following code is not working in my Rails 4 app:

Gemfile

gem 'responders', '~> 2.0'

View

<div class="field">
    <%= f.text_field :matricula, { "data-remote" => true, "data-url" => "/liquidaciones_de_viajes/check_matricula", "data-type" => :json, :size => 10 } %>
    <span id="check_matricula">&nbsp;</span>
</div>

Controller

class LiquidacionesDeViajesController < ApplicationController
        respond_to :html, :json

  def check_matricula
    @matricula = Matricula.find_by_codigo(params[:liquidacion_de_viajes][:matricula])

    # ... some process that result in matricula_ok true or false ...

    if matricula_ok
      respond_with(@matricula.empresa.razon_social)
    else
      respond_with(nil)
    end
  end
end

assets/javascripts/check_matricula.js

$(document).ready(function() {
    $("#liquidacion_de_viajes_matricula").bind('ajax:success', function(evt, data, status, xhr) {
        if (data !== null) {
            $('#razon_social').html(data.razon_social);
            $('#check_matricula').html('');
        } else {
            $('#check_matricula').html('Error de matricula');
            $('#razon_social').empty();
        }
    });

    $("#liquidacion_de_viajes_matricula").live('ajax:before', function() {
        if ($(this).val() == '') {
            return false;
        }
    });

    $("#liquidacion_de_viajes_matricula").focus(function() {
        $('#check_matricula').empty();
        $('#razon_social').empty();
    });
});

I see in console a GET is there but I did a POST in the route:

Started GET "/liquidaciones_de_viajes/check_matricula?liquidacion_de_viajes%5Bmatricula%5D=5‌​555" for 127.0.0.1 at 2016-05-30 15:00:50 -0300
Processing by LiquidacionesDeViajesController#show as JSON
Parameters: {"liquidacion_de_viajes"=>{"matricula"=>"5555"}, "id"=>"check_matricula"}
  • 写回答

2条回答 默认 最新

  • weixin_33724046 2016-05-30 14:05
    关注

    Please change your response in Json

      def check_matricula
          @matricula = Matricula.find_by_codigo(params[:liquidacion_de_viajes][:matricula])
    
      ... some process that result in matricula_ok true or false ...
    
           if matricula_ok
            respond_to do |format|
              format.html
              format.json{
                render :json => @matricula.empresa.razon_social.to_json
              }
          end
    
          else
              respond_with(nil)
          end
      end
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。