Ajax in rails returns alway error callback

风格不统一 提交于 2019-12-12 01:27:58

问题


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

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.

Here is my 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

Does anyone know what's wrong my code?

Thanks in advance.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/29726121/ajax-in-rails-returns-alway-error-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!