0

I've just tried to create new nested items(candidates) with Cocoon gem. Currently views shows not create form but update form, how can I change form.

I want to try to show just create form.

<%= form_with(model: @title) do |f| %>
    <%= f.fields_for :candidates do |candidate| %>
    <div class="candidate-item">
        <%= candidate.hidden_field :title_id, value: @title.id %>
        <div class="field">
            <label>Candidate</label>
            <%= candidate.text_field :name%>
        </div>
        <div class="field file-field">
            <label>Image</label>
            <%= candidate.file_field :image, :type => "file" %> 
        </div>
    </div>
    <% end %>

I tried it like this: form_with(model: @title, url: new_candidates_path but an undefined error occurs. Candidates nested for titles. Please let me know any ideas which shows not update form, create form.

1 Answer 1

1

To allow adding and removing items use link_to_add_association and link_to_remove_association metthods as example below

<%= form_with(model: @title) do |f| %>
  <%= f.fields_for :candidates do |candidate| %>
    <div class="candidate-item">
      <%= candidate.hidden_field :title_id, value: @title.id %>
      <div class="field">
        <label>Candidate</label>
        <%= candidate.text_field :name%>
      </div>
      <div class="field file-field">
        <label>Image</label>
        <%= candidate.file_field :image, :type => "file" %>
      </div>
      <div class="field file-field">
        <%= link_to_remove_association 'remove candidates', candidate %>
      </div>
    </div>
  <% end %>
  <div class="field file-field">
    <%= link_to_add_association 'add candidates', f, :candidates %>
  </div>
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @mehedi I can make it :)
@ShunYamada If my answer solves your problem, will you please accept it as a solution. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.