Up vote 0 down vote favorite share g+ share fb share tw.
I'm currently using the nested_form gem (Ryan Bates) to add ingredients to a recipe using a nested form. All works well. My next goal is to add autocomplete to ingredients, but I'm stuck on how I should set this up.
A recipe can have many ingredients, and an ingredient can have many recipes. I'm using a has_many :through relationship to manage the logic. Recipe.
Rb has_many :recipe_ingredients has_many :ingredients, through: :recipe_ingredients accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a:name. Blank? }, allow_destroy: true attr_accessible :name, :desc, :ingredients_attributes validates_presence_of :name ingredient.
Rb has_many :recipe_ingredients has_many :recipes, through: :recipe_ingredients attr_accessible :name, :protein, :carbs, :fat, :ingredient_name validates_presence_of :name, :protein, :carbs, :fat def ingredient_name try(:name) end def ingredient_name=(name) Ingredient. Find_by_name(name) if name. Present?
End recipe_ingredient. Rb belongs_to :recipe belongs_to :ingredient attr_accessible :ingredient_id validates_presence_of :ingredient_id, :recipe_id recipes/_form.html. Erb prohibited this recipe from being saved: Ingredients builder %> or recipes/_ingredient_fields.html.
Erb development. Log (when trying to add ingredients) Started PUT "/recipes/7" for 127.0.0.1 at 2012-03-10 12:53:15 -0600 Processing by RecipesController#update as HTML Parameters: {"utf8"=>"? ", "authenticity_token"=>"***", "recipe"=>{"name"=>"Next Recipe", "desc"=>"Only one ingredient", "ingredients_attributes"=>{"new_1331405592002"=>{"ingredient_name"=>"Beans", "protein"=>"", "carbs"=>"", "fat"=>"", "_destroy"=>"false"}}}, "commit"=>"Update Recipe", "id"=>"7"} Recipe Load (0.1ms) SELECT "recipes".
* FROM "recipes" WHERE "recipes". "id" =? LIMIT 1 "id", "7" (0.0ms) begin transaction (0.0ms) commit transaction Redirected to http://***.
Dev/recipes/7 Completed 302 Found in 2ms (ActiveRecord: 0.2ms) So! As you can see I set a new attribute named ingredient_name, and created a getter/setter method in ingredients. Rb to pull it and set it if it exists.
Basically, I think the problem is that I'm using has_many :through. It doesn't seem that what I have now would update the join table because the recipe isn't being passed to my setter method, so it wouldn't know which recipe to save it to anyway. So does the virtual attribute then need to be in the recipe model?
As you can see, just really confused. Ruby-on-rails ruby ruby-on-rails-3 nested-forms getter-setter link|improve this question edited Mar 10 at 18:54 asked Mar 10 at 18:33Steve1909 73% accept rate.
I don't think the setter method is even firing off in the implementation above. I've added a logger to it just to inspect what's going on, and even removed it, and no errors are thrown. I'm beginning to think that I'm using the wrong method name for the setter.
– Steve Mar 10 at 18:52 Added a development. Log snippet. – Steve Mar 10 at 18:55.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.