<%= form_for(@company_admin ,:url => admins_path) do |f| %>
.
.
.
.
#all fields for company admin
<%= f.fields_for :company do |company| %>
#all fields for company
.
.
<%= label_tag :Country %>
<%= autocomplete_field_tag :country ,"" , autocomplete_country_name_admins_path ,:element_id=>"#country_id",:size => 75,:class => "field" %>
</tr>
<%end %>
<%= f.submit 'Create' %>
<%end %>
我得到像
这样的参数Parameters: {"commit"=>"Create", "country"=>"india", "company_admin"=>{"company_attributes"=>{"address"=>"gt road", "category"=>"IT", "company_name"=>"google", "sub_domain"=>"classfieldidsub_domain", "telephone_number"=>"(+111) 111-111-1111"}, "gender"=>"Mr.", "last_name"=>"pqr", "email"=>"abv@gmail.com", "first_name"=>"abc"}, "authenticity_token"=>"X6bgRzJ5ydXJCyZ8+GhN57Z+O2K9gL1dsV15YQaXa84=", "utf8"=>"✓"}
即在params[:country]
我得到“印度”
我希望它应该给出:“india”的id
所以伙计们帮我解决这个问题
答案 0 :(得分:3)
您需要添加隐藏字段。这就是autocomplete将插入你的id值的地方。 然后,您可以从控制器中的params hash读取它
<%= f.hidden_field :country_id, :id => "my_country_id" %>
<%= f.autocomplete_field :country , autocomplete_country_name_admins_path ,:element_id=>"#my_country_id",:size => 75,:class => "field" %>
答案 1 :(得分:1)
尝试使用:element_id=>"#country_id"
:element_id=>"#id"
<%= f.fields_for :company do |company| %>
....
<%= f.label :Country %>
<%= f.autocomplete_field :country , autocomplete_country_name_admins_path ,:element_id=>"#id",:size => 75,:class => "field" %>
</tr>
<%end %>
我也鼓励你使用表单助手方法,而不是表单助手标记,如果它们可用:)
- 更新从rails3-jquery-autocomplete
添加一些来源 # Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) and
# that is populated with jQuery's autocomplete plugin.
#
# ==== Examples
# autocomplete_field(:post, :title, author_autocomplete_path, :size => 20)
# # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" data-autocomplete="author/autocomplete"/>
#
def autocomplete_field(object_name, method, source, options ={})
options["data-autocomplete"] = source
text_field(object_name, method, rewrite_autocomplete_option(options))
end
#
# Method used to rename the autocomplete key to a more standard
# data-autocomplete key
#
private
def rewrite_autocomplete_option(options)
options["data-update-elements"] = JSON.generate(options.delete :update_elements) if options[:update_elements]
options["data-id-element"] = options.delete :id_element if options[:id_element]
options
end
似乎autocomplete_field
生成:element_id
为html5 element data attribute
用于文本归档
再次来自宝石源
:extra_data
By default, your search will only return the required columns from the database needed to populate your form, namely id and the column you are searching (name, in the above example).
看起来他们将通过给定的element_id向对象发送消息来返回id。
答案 2 :(得分:0)
我同意上面的代码,试试看。我将要使用的代码不是形式,因为它应该与form_for帮助器。
使用form_for帮助程序创建自动完成代码后,它应该返回参数。
答案 3 :(得分:0)
您需要为隐藏字段提供一些ID,以便所选元素的ID将填充在该隐藏字段中。例如:
<%= f.hidden_field :country_id, id: 'my_country_id' %>
<%= f.autocomplete_field :country, autocomplete_country_name_admins_path ,id_element: '#my_country_id', size: 75, class: 'field' %>
对于那些想要使用Form_tag的人:
<%= hidden_field_tag :country_id, '', id: 'my_country_id' %>
<%= autocomplete_field_tag :country, autocomplete_country_name_admins_path ,id_element: '#my_country_id', size: 75, class: 'field' %>
详细说明和参考,
请访问:http://www.rubydoc.info/gems/rails3-jquery-autocomplete/0.6.2