Ajax in rails always return error callback
I posed a recent question about how to use local variable or inruby codein coffeescript instance in haml templ
Getting a helpful comment, I am trying to pass a parameter to the controller in ajax, but it always returns an error callback. I cannot 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 happened to my code?
Thanks in advance.
source to share
I think your route is wrong. It should be formatted like this:
get "posts/gaga", to: "posts#gaga"
However, this may be more than what you want if you already have resources :posts
.rb in your routes:
resource :posts do
collection do
get :gaga
end
end
Because then you can avoid repetition get "posts/..."
if you plan on adding additional custom actions.
source to share