Mark Locklear
Mark Locklear

Reputation: 5345

Rails 3 autocomplete: Whats my route?

I'm am trying to implement JQuery autocomplete in my Rails 3 app. Here are the relevant code:

view

   <%= event_form.text_field :evaluator_id, :class => "form-control" %>

js.coffee

$("#event_evaluator_id").autocomplete
        source: "presenters_path"

route

  match "events/presenters/" => "events#presenter_tokens", :as => 'presenters'

events controller

  def presenter_tokens
    if(@presenter_tokens.nil?)
      @presenter_tokens = self.presenter_connections.order(:position).pluck(:learner_id).join(',')
    end
    @presenter_tokens
  end

The error I am getting in the JS console when I start typing in the text field is:

GET http://localhost/events/2119/presenters_path?term=ma 404 (Not Found)

Upvotes: 0

Views: 39

Answers (1)

San
San

Reputation: 1954

Your problem is in the javascript. Javascript does not have access to the helper methods.

$("#event_evaluator_id").autocomplete
    source: "presenters_path"

Here you are telling javascript that source is literally "presenters_path", thats what you see in the console as being appended to the path. Change the source to actual path /events/presenters/ and it should get to the correct route.

Upvotes: 2

Related Questions