How to allow a user to add records to another record

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to allow a user to add records to another record



I need help in a Ruby on Rails application that I am working on.



I have two models (and two controllers), store (with a stores controller) and ingredient (with an ingredients controller).



These two models are associated via a HABTM (Has and Belongs to Many) association like so:


class Store < ApplicationRecord
validates :name, presence: true, length: maximum: 50 , uniqueness: case_sensitive: false
has_and_belongs_to_many :ingredients
end



And:


class Ingredient < ApplicationRecord
validates :name, presence: true, length: maximum: 50 , uniqueness: case_sensitive: false
has_and_belongs_to_many :stores
end



I have the controllers setup so that the user can add, remove, and edit stores and ingredients. But I also want to be able to add stores to an ingredient on the ingredient's edit page.



I would like to accomplish this in the view by having a text field that supports a tag like interface, like Selectize.js. Then the user can enter stores into this text field.



Is it possible to somehow pass a list of the stores that were added to the ingredient to the update action of the ingredients controller when the form is submitted? I am trying to figure out the "Rails way" to do this.



Thanks



Edit - Current Edit Form



This is my current edit form for the ingredients edit page:


<% provide :title, "Edit Ingredient" %>
<% provide :button_text, "Update Ingredient" %>
<%= render 'form' %>



And _form.html.erb


<%= bootstrap_form_for @ingredient do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description, 'data-gramm_editor' => false %>
<%= f.submit(content_for?(:button_text) ? yield(:button_text) : "Submit", class: %w[btn btn-primary]) %>
<%= link_to "Cancel", ingredients_url, class: %w[btn btn-danger] %>
<% end %>



As you can see I haven't added the ability to add a store from the ingredients edit page yet.





If you want us to help then you need to show us how you build the current edit form.. Without seeing it, it will be hard to help you.
– Arup Rakshit
Aug 10 at 18:18




2 Answers
2



It seems like you want the select tag helper. You would use options_from_collection_for_select to provide the options. If you want it to be a multiple select you'll want the name of the element to end in so that it gets sent as an array to the controller.






Oh, wow, I didn't realize that there was even such a thing as a multiple select! That could work... is there any way possible to use the selectize.js tag field, though? If not, the multiple select should work.
– Jacob Lockard
Aug 11 at 11:38






It just creates a select tag so you could use the select tag helper, add an ID option to it, and then have selectize turn it into a more interactive input.
– Michael
Aug 12 at 20:54



After trying many different options (including selectize.js, chosen, bootstrap-multiselect) I ended up solving this by using a tool called select2.



Basically I used the simple_form gem to create an association and then converted it to a "pillbox" via select2. Thanks @Michael for your help.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered