I have many different relationships between Users and Chatgroups . Intuitively, Users can have many Chatgroups , and each Chatgroup can have many users.
I implemented this using the UserChatGroup connection UserChatGroup .
In my Chatgroup controller, when it is redirected to the new function, I can rigidly indicate the number of chat members by specifying userChatGroups:[%UserChatGroup{},%UserChatGroup{}] , etc., where hardcoded number is the size of this array.
For example, my controller for the new chat group with hardcodes is 1 user:
def new(conn, _params) do changeset = ChatGroup.changeset(%ChatGroup{userChatGroups: [%UserChatGroup{}]}) render(conn, "new.html", changeset: changeset) end
and in my template for new it refers to the form. The relevant section of the form is shown below:
<%= inputs_for f, :userChatGroups, fn i -> %> <div class="form-group"> <%= label i, :user_id, "User Id", class: "control-label" %> <%= number_input i, :user_id, class: "form-control" %> </div> <% end %>
However, I would like to remove this hard-coded number of members in the chat group. Instead, I want the user to be able to dynamically add members.
I am having problems with this. Rails do this to call a function that accesses the form object and updates it. Then paste the input fields using javascript. When sending, new input fields will be fields for the new form object.
However, I cannot figure out how to do this with the Phoenix Framework.
My first question is:
- How to define functions in my
view.html.eex so that they can be called with a click, and not just displayed immediately? It seems like when I try to use <%= functionFromMyView %> , the function is displayed when my page loads. I want to call this function when I click the add button, so I need to figure out how to call this function only when clicked. Is it possible?
Question 2, more specific to my use case:
- How to add a dynamic
%UserChatGroup{} form to my form?
I assume that as soon as I can figure out how to call a function from my view when I click, I can use this function to insert %UserChatGroup{} into my form and then use javascript to insert an input field that connects to the newly inserted nested field UserChatGroup .
Everyone is welcome any guide.