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
Related
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?
My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I'm using the gem nested_form by Ryan Bates. I'm a bit confused about the workflow for the user.
I'm supposed to have a "f.link_to_add" call, that will dynamically add a nested structure for the association. That works as advertised, but I feel that it is cumbersome for the user to FIRST click "Add Book" (as in the commonly used Author/Books example) and THEN type in the new Book fields.
I would like to have an empty set of Book fields shown initially, and if the user fills them and clicks Save, the are added to the Author.
How would I use the nested_form gem to achieve this scenario?
Thx
I would suggest checking out build. For example, your new action for your author could have something like:
#author = Author.new
#author.books.build
Note that if an author could only have one book rather than multiple you'd do something like this instead:
#author = Author.new
#author.build_book
I think the short answer is that nested_form is not suitable for this particular workflow.
It's easier achieved with a separate form for the associated class where you in the controller lookup the parent class (Author) and in the Book form keep a hidden field (:author_id in this example).
This only works if you can accept to add Books one by one.
Nested_form is more suitable when you really want to show a long list of Books and let the user have the option of adding/removing multiple items before Saving.
Let's say I have a form for adding names and photos of dogs.
I would like to add up to 10 dogs on a single form.. and have any blank fields ignored...
How can I actually create such a form, and what would the controller action look like that would handle the form?
Thanks in advance
It seems like you'd want a sort of dynamic form, like Ryan Bates does on this RailsCast: http://railscasts.com/episodes/197-nested-model-form-part-2
You might want to check out the previous episode for rejecting the blank "dogs" too.
I'm building an app called "CourseWork to dig into rails/develop my skills and I have a question about how to structure it. Users have a resource called "CourseGrading" that is able to create categories and belongs to "Course". Each "category" should have a name, a percentage out of 100 and a course_id. I need to add these percentages together and alert users if the total isn't 100 while still saving.
Then the user's generated "categories" should populate an enum_string specific to that user in a resource called "CourseAssignment" which has a name, description, category and finalgrade.
Can anyone give hints or resources for how best to accomplish this? Thanks
You probably want to take a look at Active Record Callbacks. These will allow you to insert some code to be run when creating/validating/updating/deleting models.
You should probably make use of the ActiveRecord validations.
Check out this guide that explains how to write your own custom validator. Your custom validator would run when the form gets submitted, and in it, you would grab the percentage params and do your check. If it's not what you expect, you can just add an error to the form and the validation process will just kick the user back to the form page and display the error.