2010-10-17 61 views
1

我已经设法为我的Rails应用设置自动填充字段。我想要做的是让列表中的查找表的内容以及当我为几行选择查找文本时,我希望保存查找的id而不是文本本身。这是我做了什么:在Rails3中保存自动填充字段的ID而不是文本(Jquery自动完成)

Recipe模型:

class Recipe < ActiveRecord::Base 
    # validates :name, :presence => true 
    # validates :directions, :presence => true 

    # has_and_belongs_to_many :ingradients 

    has_many :ingredients_recipes 
    has_many :ingredients, :through => :ingredients_recipes 

    accepts_nested_attributes_for :ingredients_recipes, :allow_destroy => true 
end 

控制器:

class RecipesController < ApplicationController 
    # autocomplete :ndb_food_des, :long_desc 
    autocomplete :ndb_food_de, :long_desc, :limit => 15 

    ... more lines 
end 

然后我用这个视图:

<% f.fields_for :ingredients_recipes do |rif| %>   
      <td>    
       <%= rif.autocomplete_field :ingredient_id, recipes_autocomplete_ndb_food_de_long_desc_path, :width=>1000, :size=>60 %> 
      </td> 
<% end %> 

我有三个细节线在视图打开时生成,我想要做的是从自动完成中写入所选项目的ID。如果我在视图中指定了正确的字段作为ingredient_id,那么我总是在插入的id中得到0。如果我指定了其他字段,我会得到自动完成的完整文本,所以看起来Rails正在拾取正确的字段并似乎知道该怎么做......但它没有得到正确的ID。

这里的系统做什么痕迹:

Started POST "/recipes" for 127.0.0.1 at 2010-10-17 16:28:10 +0000 
    Processing by RecipesController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"wmNQbsGoiGccrNpmFs7r7D7ZBq//SBe9yrk6SFXP0lc=", "recipe"=>{"name"=>"dfsa", "description"=>"sdfa", "directions"=>"sadf", "ingredients_recipes_attributes"=>{"0"=>{"ingredient_id"=>"CAMPBELL Soup Company, CAMPBELL'S Beef Gravy", "readable_qty"=>"30", "description"=>""}, "1"=>{"ingredient_id"=>"Eclairs, custard-filled with chocolate glaze, prepared from recipe", "readable_qty"=>"60", "description"=>""}, "2"=>{"ingredient_id"=>"DENNY'S, MOONS & STARS chicken nuggets, from kid's menu", "readable_qty"=>"10", "description"=>""}}, "servings"=>"3", "time"=>"3"}, "commit"=>"Create Recipe"} 
    SQL (27.9ms) INSERT INTO "recipes" ("created_at", "description", "directions", "name", "owner", "servings", "time", "updated_at") VALUES ('2010-10-17 16:28:10.686644', 'sdfa', 'sadf', 'dfsa', NULL, 3, 3, '2010-10-17 16:28:10.686644') 
    SQL (0.2ms) INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.729542', '', **0,** 32.0, 18, '2010-10-17 16:28:10.729542') 
    SQL (0.1ms) INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.783068', '', **0,** 62.0, 18, '2010-10-17 16:28:10.783068') 
    SQL (0.1ms) INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.784333', '', **0,** 12.0, 18, '2010-10-17 16:28:10.784333') 
Redirected to http://0.0.0.0:3000/recipes/18 
Completed 302 Found in 190ms 


Started GET "/recipes/18" for 127.0.0.1 at 2010-10-17 16:28:10 +0000 
    Processing by RecipesController#show as HTML 
    Parameters: {"id"=>"18"} 
    Recipe Load (0.4ms) SELECT "recipes".* FROM "recipes" WHERE ("recipes"."id" = 18) LIMIT 1 
    IngredientsRecipe Load (0.3ms) SELECT "ingredients_recipes".* FROM "ingredients_recipes" WHERE ("ingredients_recipes".recipe_id = 18) 
    Ingredient Load (0.2ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
    CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
    CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
    CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
    CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
    CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1 
Rendered recipes/show.html.erb within layouts/application (112.1ms) 
Completed 200 OK in 143ms (Views: 117.4ms | ActiveRecord: 0.9ms) 

我已经做了文字在上面在那里的文字的书写,而不是从自动完成的ID的例子大胆。我不知道如何设置它来使用id。

帮助将不胜感激。

回答

0

您可以使用:id_element获取选定元素的ID