nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 383 forks source link

association in controller for show nested forms #592

Closed Halvanhelv closed 3 years ago

Halvanhelv commented 3 years ago

Hi, do I need to create an association in the controller like questions.links.new to display the form, without using a library it is required to show the form and for cocoon do I need to do this?

nathanvda commented 3 years ago

Not entirely sure why you ask this, as afaik this is easily to test and confirm. So

But I am not sure I understood you correctly. If you show me some code, or explain your actual case in more detail (is the link required? If you can have only one link, why use a has_many (you wrote the plural form). If they can have more, how will the users add extra links (dynamically in one form --use cocoon). But there are many ways to add nested items to a parent, cocoon is only one of them.

Halvanhelv commented 3 years ago

Not entirely sure why you ask this, as afaik this is easily to test and confirm. So

  • if you want to show a form with one (empty) link, do something like @questions.links.build in the controller new action
  • if you do not need to add more links dynamically, you do not need cocoon

But I am not sure I understood you correctly. If you show me some code, or explain your actual case in more detail (is the link required? If you can have only one link, why use a has_many (you wrote the plural form). If they can have more, how will the users add extra links (dynamically in one form --use cocoon). But there are many ways to add nested items to a parent, cocoon is only one of them.

Sorry for my bad English, I mean without cocoon I need to create an object to display any form, but does cocoon work without creating an object in "action"?

Halvanhelv commented 3 years ago

If I didn't write @ questions.links.build in "new" I can't see the form, later I added cocoon and the form works without @ questions.links.build, does the cocoon require @ questions.links.build to work correctly?

nathanvda commented 3 years ago

It seems obvious to me, but: no, cocoon does not require the pre-building of the nested items, as users of your website can dynamically add them using the link_to_add_association link. This greatly depends on the wanted user experience: a lot of sites prefer to already show one empty nested item (if users will add at least one anyone --one click less), but technically there is no need.

Of course, if you seem to have the experience your website does not work without the @question.links.build there is a very common error where the link_to_add_association is rendered inside the nested child partial (due to not entirely comprehending how haml works). For clarity one could always check the ERB example code here.

Halvanhelv commented 3 years ago

It seems obvious to me, but: no, cocoon does not require the pre-building of the nested items, as users of your website can dynamically add them using the link_to_add_association link. This greatly depends on the wanted user experience: a lot of sites prefer to already show one empty nested item (if users will add at least one anyone --one click less), but technically there is no need.

Of course, if you seem to have the experience your website does not work without the @question.links.build there is a very common error where the link_to_add_association is rendered inside the nested child partial (due to not entirely comprehending how haml works). For clarity one could always check the ERB example code here.

thanks for the answer