首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax请求不工作rails 4

Ajax请求不工作rails 4
EN

Stack Overflow用户
提问于 2016-05-30 13:39:10
回答 2查看 137关注 0票数 0

我试图在字段出口处点击数据库,而不使用Ajax重新加载表单。控制器必须对从数据库获取的数据进行一些处理,并响应视图。然后,用户将继续输入。

我错过了一些东西,只是看不出是怎么回事。我有相同的代码在Rails 3.2应用程序中工作,没有问题。在Rails 3.2中,请求转到check_matricula方法,但在Rails 4中,请求转到show操作。

下面的代码在我的Rails 4应用程序中不起作用:

Gemfile

gem 'responders', '~> 2.0'

视图

代码语言:javascript
复制
<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>

控制器

代码语言:javascript
复制
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

代码语言:javascript
复制
$(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();
    });
});

我在控制台看到一个GET在那里,但我在路线上发了个帖子:

代码语言:javascript
复制
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"}
EN

回答 2

Stack Overflow用户

发布于 2016-05-30 14:05:38

请更改您对Json的答复。

代码语言:javascript
复制
  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
票数 0
EN

Stack Overflow用户

发布于 2016-05-30 16:11:56

您只需为您的contoller指定check_matricula路由:

代码语言:javascript
复制
resources : liquidaciones_de_viajes do
  collection do
    post `check_matricula`
  end
end

如果没有此路由,Rails假定check_matriculashow路由的:id部分。

要通过POST将请求发送到URL,请将data-method: :post选项添加到text_field中,如下所示:

代码语言:javascript
复制
<%= f.text_field :matricula, { data-remote: true, data-url: "/liquidaciones_de_viajes/check_matricula", data-type: :json, data-method: :post, size: 10 } %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37527147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档