阿贾克斯在轨返回送花儿给人错误回调 [英] Ajax in rails returns alway error callback

查看:120
本文介绍了阿贾克斯在轨返回送花儿给人错误回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我贴的问题recentry为相同 <一href="https://stackoverflow.com/questions/29725095/how-to-use-local-or-instance-variable-in-ruby-$c$c-in-coffeescript-in-haml-templ">how使用本地或实例变量inruby codeIN的CoffeeScript在HAML TEMPL

I posted question recentry as same how to use local or instance variable inruby codein coffeescript in haml templ

获取有用的意见,我想传递参数在Ajax来控制器,但它总是返回错误回调,我不能没有找到原因。

Getting helpful comment, I'm trying to pass param to controller in ajax but it always returns error callback I can't not find the reason.

下面是我的code。

.html.haml

:coffee
$('input#field').change ->
    $.ajax
        url: '/posts/gaga'
        type: "GET"
        dataType: "json"
        data: { code: $('input#field').val() }
        error: (jqXHR, textStatus, errorThrown) ->
            alert "error"
        success: (data, textStatus, jqXHR) ->
            alert "success"

routes.rb中

get 'posts/gaga'

posts_controller.rb

def gaga
    @model = Model.new
    render nothing: true
end

有谁知道什么是错我的code?

Does anyone know what's wrong my code?

在此先感谢。

推荐答案

我觉得你的路线不正确。它应该被格式化这样至少有:

I think your route is incorrect. It should be formatted like this at least:

拿到上岗/加加,以岗位#加加

不过,这可能是更多你想,如果你已经有一个的资源是什么:帖子在routes.rb中:

However this might be more what you want if you already have a resources :posts in your routes.rb:

resource :posts do
  collection do
    get :gaga
  end
end

由于那么你就可以避免重复拿到上岗/..."如果您打算增加更多的自定义操作。

Because then you can avoid repeating get "posts/..." if you plan on adding more custom actions.

这篇关于阿贾克斯在轨返回送花儿给人错误回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆