Create group of rows in rails - ruby-on-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

Related

Ruby on Rails: Nested form with Existing Objects

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?

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.

ActiveAdmin -- How to create join table records?

I have two categories merchants and aliases who have a join table called aliases_merchants
In ActiveAdmin, how can I display a textbox whose content will get entered into the joining table?
I'm able to display the textbox but upon post it doesn't create the record in the joining table in the db.
Ideally, I would love to be able to get the functionality like the following (but I feel ActiveAdmin wouldn't be able to do it)...
I solved this by creating a sidebar and two member functions in the ActiveAdmin resource to accomplish this. Works just described above.

Dropdown Box Ruby on Rails question

I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.

Using a select field with Rails nested object forms

Is it possible to use select fields with nested object forms feature of Rails 2.3?
Example:
Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories.
On our Edit Article form, you want to have an HTML select list of all the available categories. The user can select one or more Category names to assign to the Article (multiple select is enabled).
There are lots of ways to do this, but I'm wondering if there is a simple way to accomplish this using the nested objects feature. What would the form look like in your view?
Check out the nested form example from Github:
http://github.com/alloy/complex-form-examples
It's been a while since I looked at it, so I'm not sure if it covers exactly what you wanna do, but its a nice source for ideas / patterns.
Assuming you have defined the models and their relationships so you can do this:
#art = Article.find(article_id)
#art.categories # returns list of category objects this article is assigned to.
Then I usually use http://trendwork.kmf.de/175
You need to copy the JavaScript file into public/javascripts but after that you can just create the form element with something like:
swapselect(:article,#art,:categories,Category.find(:all).map { |cat| [cat.name, cat.id] })
(I would tend to wrap that in a helper to make the call even cleaner)
One small gotcha is that for very long lists it can run a little slow in IE6 because there's quite a lot of appendChild calls in the js which is notorioulsy slow in IE6
Update: Apologies. This doesn't really answer your original question, which was specifically about the Rails 2.3 feature. The swapselect option is version independent and doesn't make use of newer Rails functionality.

Resources