In my application, users belong to a group, and this is done in my user group model.
class Usergroup < ApplicationRecord belongs_to :user belongs_to :group end class User < ApplicationRecord has_many :usergroups has_many :groups, through: :usergroups end class Group < ApplicationRecord has_many :usergroups has_many :users, through: :usergroups end
When I want to add user to group , although I need to know the group identifier and the user identifier, which is less than ideal. I created an autocomplete field using jQuery to take care of this for me.
<%= form_with(model: usergroup, local: true) do |form| %> <div class="field"> <%= form.label :user_id %> <%= form.text_field :user_id, id: 'user', data: { autocomplete_source: User.order(:email).map(&:email) } %> </div> <%= form.submit %> <% end %>
When I try to create a new usergroup with the email address that I selected from this drop-down menu, it is sent because it requires the user to be a user. How do I pass the complete user object to this field, and not just the email address of the user I want to create? Am I right about this?
This is the route that is used to get users.
user GET /users/:id(.:format) users
ruby-on-rails
Trenton tyler
source share