2 none nested model in 1 form rails - ruby-on-rails

Does any one know how to make 2 none nested models work on a single form for rails 4? For example, I have user model that has one profile(model) and one car(model).
Both profile and car models are nested under user but car and profile has no relationship. I figure I can do a form_for user and use both field_for profile/car but is there a better way to do this?

In this situation I would create a form object. Check out point '3. Extract Form Objects' in the following post: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

Related

Submitting two tables/model data with single/two form on one webpage with single submit button?

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

How to associate a user in a Rails nested model form?

Given the typical nested model example from Railscasts:
http://railscasts.com/episodes/196-nested-model-form-part-1
How would you associate the answers to a user?
Rephrased, if you're building a model through another model, how would you associate a user for every child element created?
Rephrased yet again with another example:
If I had Albums and Pictures and I created many Pictures at once through the Album's controller associating it to that Album, how would I associate every Picture in that same update_attributes call or whatever the most idiomatic way for Rails is?
I ended up using the following to make it work:
params[:exercise][:log_entries_attributes].each do |value|
value[1].merge!(:user_id => current_user.id)
end
Is there a more Rails or Ruby appropriate version of handling this?

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

Preventing duplicate model objects with nested forms in rails 3

I have a form set up for a Recipes model that accepts_nested_attributes_for Ingredients. The form and saving works fine, however, I want to check for the existence of each Ingredient in the database. The form seems to be creating new records for ingredients, even if they already exist in the database.
Recipes and Ingredients are related using a 'has_many :through' relationship. Is there a clean way to make sure that ingredients are duplicated when the form is submitted, but the relationships are still established?
similar question might help you on this, although the solution is not that eligant
Rails nested form on many-to-many: how to prevent duplicates?
It is about how you are taking input from the user - your forms are.
If you have a multiple select input for ingredients in new/edit form of recipe and you show all the existing ingredients there, user can choose the existing ingredients and the POST/PUT from these forms will have the existing ingredients' ids as part of form data. This will not create new ingredients then.

Build rails form fields from associated models

Rails3,jQuery. What I'm trying to do: Create a new WorkoutPlan that HABTM Exercises. Then create a new Workout that belongs_to a WorkoutPlan. I want the Workout#edit page to build a form with fields_for new/edit WorkoutRoutines, one routine for each exercise in the WorkoutPlan.
That last part is where I'm struggling, creating one Routine for each Exercise in the WorkoutPlan. Everything else works, its just this form build that sucks. Not sure if I'm making this overly complicated, or if there was an easier way. Any ideas?
From what I understand, using nested attributes should solve the problem without many complications. From the link,
Nested attributes allow you to save
attributes on associated records
through the parent. By default nested
attribute updating is turned off, you
can enable it using the
accepts_nested_attributes_for class
method. When you enable nested
attributes an attribute writer is
defined on the model.
The attribute writer is named after
the association, which means that in
the following example, two new methods
are added to your model:
author_attributes=(attributes) and pages_attributes=(attributes).
anytime you need a complex form in rails its either going to be a pain, or can use formtastic. super easy awesome forms, plenty of ways to deal with habtm relations as well.

Resources