Rails 4 - Update Join Table Attributes with has_many through -


this follow-up of question: rails 4 - access join table value in views

now know how retrieve join table attributes , show them in view. still can't find right approach edit , update attribute.

models:

class recipe < activerecord::base   has_many :recipe_ingredients   has_many :ingredients, through: :recipe_ingredients end  class ingredient < activerecord::base   has_many :recipe_ingredients   has_many :recipes, through: :recipe_ingredients end  class recipeingredient < activerecord::base   belongs_to :recipe   belongs_to :ingredient    #there attribute t.text :amount end 

recipe#edit:

... <%= simple_form_for @recipe |f| %>     <div class="form-group">       <%= f.input :name, input_html: { class: "form-control"} %>       <%= f.input :description, input_html: { class: "form-control"} %>       <%= f.collection_select(:ingredient_ids, ingredient.all, :id, :title, {}, {multiple: true}) %>       <!-- reserve recipe_ingredients.amount -->     </div>     <%= f.submit "submit", disable_with: 'submitting...', class: "btn btn-primary"%>   <% end %> ... 

as shown above, many-to-many relationship, each recipe may have several ingredients. works fine , can choose right models associate (using collection_select). have left work edit , update join table attributes undone. have 2 ideas in mind:

  1. make edit page editing join model attributes
  2. ajax? not quite familiar with

i know question seems trivial, many solutions out-dated(rails 3). desperate rails 4 solution.

edit

intention: make creation of recipe , ingredient models on own, done. user see recipe using ingredient in ingredient#show action, done also. recipe#show action, there slight difference, user see ingredient using amount(join table attribute), done in previous question. last problem is, make attribute editable.

you may want doing nested models combined cocoon gem. allow have form updates multiple models @ once , takes care of ajax you. https://github.com/nathanvda/cocoon. used gem on system user needed able add 1 or more answers trivia question via dynamic form fields. think work giving user ability add 1 or many recipeingredient relations recipe. able add selects dynamically form via cocoon containing select ingredient model records.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -