Ruby on Rails: Nested form with Existing Objects - ruby-on-rails

I want to create a nested form in Ruby on Rails. Let's say I have a Model Player and a Model Achievement.
In a form for the User I want to assign n Achievements to the User. The Achievements have been created before, so they exist when the Achievements get assigned. How do I do that? How should the View, the Controller and the Models look like? I searched for hours but I didn't find anything.
I'd like to realize it with a select field, with which the Achievements get selected.

there is quite a few examples out there and depending which form are you using you can choose:
https://stevepolito.design/blog/create-a-nested-form-in-rails-from-scratch/
https://levelup.gitconnected.com/the-many-things-about-nested-form-in-ruby-on-rails-15f32ed66446
https://tudip.com/blog-post/the-implementation-of-nested-forms-in-ruby-on-rails-fields-for/#:~:text=Rails%20provide%20a%20powerful%20mechanism,the%20parent%20on%20associated%20records.
you can also use simple_forms https://github.com/heartcombo/simple_form which will automatically create a select field for the achievements (you can check their documentation) and this might help How should I use rails and simple_form for nested resources?

Related

How do I create a rails simple_form with variable input fields

I have a PurchaseOrder Model that has_many Items. The form for PurchaseOrder needs variable input fields that will also save Items, where clicking the Add button will increase the fields in the page.
Here's what it will look like:
In order to achieve this:
How do I create a simple_form that will post the result of these multiple fields as an array to my existing PurchaseOrdercontroller where I can process and add these records?
Bonus: how would I handle this via Cucumber?
Edit: Why would you downvote a self contained question? Leave comments to explain when you downvote, please.
Use Cocoon as your nested forms for creating multiple fields.
You need to use simple_fields_for from simple_form gem. This allows you to work with attributes of associated models.
If you want to add new associated models via Add new button you need to create a new row. There is no code in pure simple_form that will help you with that. I found gem cocoon. It looks like it's what you're looking for. You can take a look how it's implemented there and make your own light solution.

Create group of rows in rails

I have a simple rails application with a table created using the rails generate scaffold command. It allows the user to add the names of different movies using a form i.e. automatically generated by rails. I want to add an option where a User can add the name of a director like Christopher Nolan and then add the name of all the movies of that director within it, sort of like a group in the rows. Also all the subrows need to be linked to each other. Is this possible on rails?
P.S. I know that Stack Overflow works on the concept where I add a snippet of code for help, but since I'm a newbie I don't have any idea about how to even start with this
About nested model form http://railscasts.com/episodes/196-nested-model-form-part-1
And this gem can help u for create form with list items
https://github.com/nathanvda/cocoon

In a single form, linking two instances of two models with habtm

I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly

Mass edit in Ruby on Rails

Let's say you have some products which have 2 fields, a description and a stock value.
How do you layout a form of all the products in an action named mass_edit with fields so a user can edit some, press submit and then save them in a mass_update?
I have used nested forms in a order-order_line scenario but i can't seem to figure this one out.
The following screencasts can help you to find a solution:
http://railscasts.com/episodes/165-edit-multiple
http://railscasts.com/episodes/198-edit-multiple-individually

Friendship invite form with hobo

The hobo lifecycle tutorial shows how to implement a friendship logic in
the model and controller. However it does not really cover how to glue
the gui/views together. When I go to /friendships/invite - hobo
presents me with a form with a drop down menu. How do I add a form to
the user show-page with just one button (Invite) I guess that the the
user viewed should be in a hidden field?
I tried adding the form like this:
<extend tag="show-page" for="User">
<old-show-page merge>
<append-content-body:>
<invite-form for="Friendship" />
</append-content-body:>
</old-show-page>
</extend>
Hobo ignores the invite-form hmmm I must be missing something.
Best regards
Asbjørn Morell
try <invite-form:friendships.build/> assuming you have has_many :friendships on your user model. for is an attribute on the 'def' and 'extend' tags, it's not recognized by 'form'. To use a polymorphic tag, simply pass in an instance of the correct type in the context. This of course is easier if an instance already exists, but since you're using a creator action, just build one.
P.S. Questions like this get answered much quicker on the Hobo Users mailing list.

Resources