Ruby on rails Rails 3自动完成gem与所属关联
我使用的是,当将它用于模型中存在的字段时,它的效果非常好。然而,我一直在尝试将其用于关联,但我看不到如何使其工作。那么让我们来解释一下我所拥有的: 型号:Ruby on rails Rails 3自动完成gem与所属关联,ruby-on-rails,autocomplete,associations,Ruby On Rails,Autocomplete,Associations,我使用的是,当将它用于模型中存在的字段时,它的效果非常好。然而,我一直在尝试将其用于关联,但我看不到如何使其工作。那么让我们来解释一下我所拥有的: 型号: class Receipt < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :receipts end <%= form_for(@receipt, :url => admin_receip
class Receipt < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :receipts
end
<%= form_for(@receipt, :url => admin_receipt_path, :html => { :multipart => true }) do |f| %>
<div class="clearfix">
<%= f.label :value, "Value($)" %>
<div class="input"><%= f.text_field :value %></div>
</div>
<div class="clearfix">
<%= f.label :user_id, "User" %>
<div class="input">
<%= f.autocomplete_field :user_id, autocomplete_user_name_admin_receipts_path %>
</div>
</div>
.....
类收据
控制器
class Admin::ReceiptsController < 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
class Admin::receiptscocontroller
查看(表格):
class Receipt < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :receipts
end
<%= form_for(@receipt, :url => admin_receipt_path, :html => { :multipart => true }) do |f| %>
<div class="clearfix">
<%= f.label :value, "Value($)" %>
<div class="input"><%= f.text_field :value %></div>
</div>
<div class="clearfix">
<%= f.label :user_id, "User" %>
<div class="input">
<%= f.autocomplete_field :user_id, autocomplete_user_name_admin_receipts_path %>
</div>
</div>
.....
admin_receive_path,:html=>{:multipart=>true})do | f |%>
.....
问题是。。。我可以获取用户名,但我想实际将用户id存储在其中。当管理员试图编辑与关联用户关联的现有收据时,我希望以相同的方式显示名称。我可以使用此下拉列表执行以下操作:
<div class="clearfix">
<%= f.label :user_id, "User" %>
<div class="input"><%= f.select :user_id, @users.collect {|p| [ p.name, p.id ] },{:prompt => 'Select a User'} %></div>
</div>
'选择用户'}%>
我看不出我该如何使用这颗宝石……瑞安·贝茨(Ryan Bates)做了一个关于这颗宝石的放映。干净利落
如果您不想订阅,请尝试torrent控制器的代码在哪里?已添加。尽管我相信这并不能为我试图解决的问题提供很多信息……你不能描述一下他的方法吗?