Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 autocomplete gem with belongs_to association
    primarykey
    data
    text
    <p>I am using the <a href="https://github.com/crowdint/rails3-jquery-autocomplete" rel="nofollow">rails3-jquery-autocomplete gem</a> and it works neat when using it for fields that exist in the model. However, I have been trying to use it for associations and I fail to see how to make it work. So let's explain what I have:</p> <p><strong>Models</strong>:</p> <pre><code>class Receipt &lt; ActiveRecord::Base belongs_to :user end class User &lt; ActiveRecord::Base has_many :receipts end </code></pre> <p><strong>Controller</strong></p> <pre><code>class Admin::ReceiptsController &lt; AdminController autocomplete :user, :name def index @receipts = Receipt.all end def show @receipt = Receipt.find_by_id(params[:id]) end def edit @receipt = Receipt.find_by_id(params[:id]) @users = User.all end end </code></pre> <p><strong>View (form)</strong>:</p> <pre><code>&lt;%= form_for(@receipt, :url =&gt; admin_receipt_path, :html =&gt; { :multipart =&gt; true }) do |f| %&gt; &lt;div class="clearfix"&gt; &lt;%= f.label :value, "Value($)" %&gt; &lt;div class="input"&gt;&lt;%= f.text_field :value %&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="clearfix"&gt; &lt;%= f.label :user_id, "User" %&gt; &lt;div class="input"&gt; &lt;%= f.autocomplete_field :user_id, autocomplete_user_name_admin_receipts_path %&gt; &lt;/div&gt; &lt;/div&gt; ..... </code></pre> <p>The thing is... I am able to fetch user names, but I want to actually store the user id in there. The same way I would like to show the name when the admin tries to edit an existing receipt with a user associated. Something that I am able to do with this drop down:</p> <pre><code>&lt;div class="clearfix"&gt; &lt;%= f.label :user_id, "User" %&gt; &lt;div class="input"&gt;&lt;%= f.select :user_id, @users.collect {|p| [ p.name, p.id ] },{:prompt =&gt; 'Select a User'} %&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>I am failing to see how would I do this with this gem....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload