【问题标题】:Ajax in rails returns alway error callbackrails中的Ajax总是返回错误回调
【发布时间】:2015-06-25 21:27:05
【问题描述】:

我发布了同样的问题 how to use local or instance variable inruby codein coffeescript in haml templ

得到有用的评论,我正在尝试将参数传递给 ajax 中的控制器,但它总是返回错误回调,我找不到原因。

这是我的代码。

.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

有人知道我的代码有什么问题吗?

提前致谢。

【问题讨论】:

  • 返回什么错误?

标签: ruby-on-rails ruby ajax coffeescript


【解决方案1】:

我认为您的路线不正确。至少应该这样格式化:

get "posts/gaga", to: "posts#gaga"

但是,如果您的 routes.rb 中已经有 resources :posts,这可能是您想要的更多:

resource :posts do
  collection do
    get :gaga
  end
end

因为如果您计划添加更多自定义操作,则可以避免重复 get "posts/..."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    相关资源
    最近更新 更多