views:

500

answers:

2

Where do I reference my controller (Rails) URL to show the dataset that I want in the autocomplete via JQuery? Here is my head:

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"&gt;&lt;/script&gt;
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"&gt;&lt;/script&gt;
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"&gt;&lt;/script&gt;
  <script>
  $(document).ready(function(){
    var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
$("#example").autocomplete(data);
  });
  </script>

Trying to figure out how to reference my controller or model (whichever I should) to get only records that are associated with a specific user_id. Just some frame of reference.

I have three tables, Users, Prospects, and Notes. Trying to set it up so that a specific user (user_id) can "add a note" and then use an autocomplete field to help "tag" to a prospect they have previously entered. I have already set up authentication and it is all working. JQuery seems to be getting me the closest. The head is above, and also I have uploaded jquery-1.3.2.js (though no reference to it yet as you can see in the head). Here is my prospects controller code:

class ProspectsController < ApplicationController

  before_filter :login_required

  # GET /prospects
  # GET /prospects.xml

   def index
    @prospects = current_user.prospects

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @prospects }
    end
  end

  # GET /prospects/1
  # GET /prospects/1.xml
  def show
    @prospect = current_user.prospects.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @prospect }
    end
  end

  # GET /prospects/new
  # GET /prospects/new.xml
  def new
    @prospect = Prospect.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @prospect }
    end
  end

  # GET /prospects/1/edit
  def edit
    @prospect = current_user.prospects.find(params[:id])

    respond_to do |format|
      format.html # edit.html.erb
      format.xml  { render :xml => @prospect }
    end
  end

  # POST /prospects
  # POST /prospects.xml
  def create
    @prospect = current_user.prospects.create(params[:prospect])

    respond_to do |format|
      if @prospect.save
        flash[:notice] = 'Prospect was successfully created.'
        format.html { redirect_to(@prospect) }
        format.xml  { render :xml => @prospect, :status => :created, :location => @prospect }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @prospect.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /prospects/1
  # PUT /prospects/1.xml
  def update
    @prospect = current_user.prospects.find(params[:id])

    respond_to do |format|
      if @prospect.update_attributes(params[:prospect])
        flash[:notice] = 'Prospect was successfully updated.'
        format.html { redirect_to(@prospect) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @prospect.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /prospects/1
  # DELETE /prospects/1.xml
  def destroy
    @prospect = Prospect.find(params[:id])
    @prospect.destroy

    respond_to do |format|
      format.html { redirect_to(prospects_url) }

    end
  end
end
A: 

try jquery autocomplete. i don't know anything about rails, but what you need to return to autocomplete should be easy enough even for a beginner.

Brandon H
+1  A: 

There's always Ryan Bate's Railscast on that subject. He's using the standard Rails autocomplete.

However, I prefer to use jQuery. I've used Dylan Verheul's autocomplete recently and found it very easy to set up.

agregoire
Can you do the record level authentication as I mentioned above?
bgadoci
The javascript part itself does not know anything about authentication. But you can have the autocomplete call the url you want with the parameters you want to get its list of suggestions. That way, the authentication will be handled by your controller or your model.
agregoire
That sound like it would work. Where would you insert the URL to call? Is it in the javascript header? If so which one? Or does it need to be in the form? Conceptually I get it, just need a little help on construction. Appreciate the help man...newbie here.
bgadoci
I am assuming it is somewhere in the head, just where.
bgadoci
I updated the question to show my header.
bgadoci
The jQuery autocomplete plugin accepts either an array or a url as the first option. So you can call it like that: $("#example").autocomplete('/resources');If you type 'foo' in the text field, the plugin will call /resources?q=foo.There are other options you can use. The documentation is on jQuery's site (http://docs.jquery.com/Plugins/Autocomplete).And, yes, you can put that javascript in the head of your document.
agregoire