http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
This post helped in learning how to handle multiple models in a rails form. It works as long as the models are nested. what if they are not? lets say, I have a form, where the user fills personal details, address details and a bunch of checkboxes specifying her interests. There are at least 3 tables involved in this one single form, what is the best way to handle this, without having 3 different save buttons?
Two options:
First is ActivePresenter which works well for this.
Second is just to use fields_for:
<%= form_for #user do |f| %>
<%=f.label :name %>
<%=f.text_field :name %>
<%= fields_for #address do |fa| %>
<%=fa.label :city %>
<%=fa.text_field :city %>
<% end %>
<% end %>
Then in the controller, save the records.
#user = User.new(params[:user])
#address = Address.new(params[:address])
ActivePresenter works so well though.
Also found a railsforum post via Google, which would work well.
You can refer this tutorial by The Pragmatic Programmers
Advanced Rails Recipes
Related
I've got a relatively complex form I'm trying to code efficiently. Most online examples of nested forms deal with very clear hierarchical relationships, mine does not.
Below is the data model. The essential job of the form is to create a "Job Entry" record while at the same time creating a new "Entity" record - which is a person. Several relationships come to bear in this form.
A "Job" is already created. The Job has 1-to-many "Questions" which exist before the user hits this form. However, they must fill in "Answers" to the questions. They also choose one of many pre-created "Job Roles".
The question is how to leverage "form_with" and "fields_for" for all these inter-related models.
My assumption is to ditch built-in helpers and just use a form_tag and roll everything together manually. But maybe there is a "correct" way to roll forms that do not necessarily abide by parent-child relationships? In my example, there is no pure top-level object to start with since many child objects already have records, but maybe I am wrong and Entity should be the starting point?
Entity has_many Job_Roles
Entity has_many Job_Entries
Job has many Job_Roles
Job has_many Job_Entries
Job has_many Questions
Question has many Answers
Answers belong_to Entity
Agency has_many Job_Entries
etc...
There is no need to ditch the built-in helper: Rails has thought about that, it's called nested forms.
Here is an example:
<%= form_with model: #job do |f| %>
Job entries:
<ul>
<%= f.fields_for :job_entries do |je_form| %>
<li>
<%= je_form.label :kind %>
<%= je_form.text_field :kind %>
<%= je_form.label :street %>
<%= je_form.text_field :street %>
...
</li>
<% end %>
</ul>
<% end %>
You can nest as many children forms as you'd like using fields_for. Don't forget to use accepts_nested_attributes_for in the parent models.
Nested forms as Mike proposed are a rails-way solution of your problem. It is ok - but for complex forms, with lot of validations, it may not be the best solution). You could consider using a FormObject pattern instead.
FormObject is a simple ruby class that uou can keep it i.e. in Forms folder and use as below:
class JobEntryForm
include ActiveModel::Model
attr_accessor :customer_id, :agency_id, :name, :question_text #you can use atributes from different models
validates :customer_id, presence: true #you can validate yu attributes as you want - your in necessity to use model validation
def initialize(attributes:)
#customer_id = attributes[:customer_id]
#agency_id = attributes[:agency_id]
#name = attributes[:name]
#question_text = attributes[:question_text]
end
#implement whatever you need
end
than in you controller:
#form = JobEntryForm.new
and you your view:
<%= form_for #form do |f| %>
<%= f.label :customer_id, 'Customer' %>:
<%= f.text_field :customer_id %>
...
<%= f.submit %>
<% end %>
And - at the end - in your controller create method:
def create
#form = CreateJobEntry.new.call(attributes: form_params) #service object to keep your controller clean.
end
I'm rather new to Ruby, so I need guidance as how to approach this. I know how to make very simple forms, but I'm thinking of implementing a custom form that can generate a payroll for a business during a specific start and end date. I already have business and payroll models, but I'm not sure how to implement this type of custom form and properly route it to get it be fully functional. Are there any resources or pieces of guidance I can get to help me with this?
please checkout this link :create an application on RoR. Please go to the link above and get started by creating your very own application. I have started with this and have learned a lot from this. This will get you kick started on RoR. But I believe you need more guidance in Ruby also. So I am attaching some more useful links. Please check them out also.
tutorails point - ruby
ruby-lang documentation
ruby guides
Thank you.
I don't know if I really get your issue... If your models Business and Payroll are linked (with :belongs_to and :has_many) you should check the fields_for method. You can nest it in a form_for/form_with to create fields associated to an other model. For instance you can do something like that:
#in your view
<%= form_with model: #business |f| %>
<%= f.label "Something:" %>
<%= f.text_field :something %>
<%= f.fields_for #business.payroll do |ff| %>
<%= ff.label "Something else:" %>
<%= ff.text_field :something_else %>
<% end %>
<%= f.submit %>
<% end %>
#in your controller
if #business.create(business_params)
#...
end
if #business.payroll.create(payroll_params)
#...
end
Hope it will help you !
I am working on a rails form. Essentially, a person can have multiple statuses and switch between the different statuses. In database table, the display will be simple as follows:
status start_date end_date
work 1/1/15 1/10/15
sick 1/11/15 2/15/15
work 2/16/15 3/15/15
sick 1/15/15 1/14/15
I need to prompt user to input these information. I have made a status class which belongs to a person class. So basically, these fields will be a part of nested forms.
My question is: How can I dynamically display these information to make forms elegant and clean to use?
Thanks!
If I understood your domain, your Person has many Status, right?
The simplest way to do it is use the gem cocoon. Your view will look like this:
<%= form_for #person do |person_form| %>
<%= person_form.input :name %>
<%= person_form.fields_for :statuses do |status_form| %>
<%= status_form.field :start_date, :end_date %>
<!-- cocoon's method to dynamically add nested forms -->
<%= link_to_add_association 'add status', person_form, :statuses
<% end %>
<% end %>
I have a view with 3 forms, Schedules, Workouts and Exercises, all behaving like an edit form, each. And one submit(save) button in the all the view.
When I click on the save button. Every data changed on those forms should be updated after click.
What is the best solution for this ? Javascript updating each data separated ? How to do that ? Is there a more Rails way to do this easily ?
My difficulty is how to integrated all those models in one view, while all this is happening in the show(view) from the Student model.
If you're implementing something like a profile / edit page (where you can save all the records at once), the two ways I would look at would either be to save the forms via Ajax, or use a single submit method to handle them
Ajax
The ajax method would be the most conventional:
Every form you submit will go to the form's own update method in the backend
Each form could be handled by a single button, but it's best to split them up
#app/controllers/profile_controller.rb
def edit
#schedules = Schedule.all #-> not sure how many records you're using
#workouts = Workout.all
#exercises = Exercise.all
end
#app/views/profile/edit.html.erb
<%= form_for #schedule do |f| %>
<%= f.text_field :test %>
<% end %>
# -> other forms
<%= button_to "Save", "#", id: "save" %>
#app/assets/javascripts/application.js
$("#save").on("click", function() {
$("form").submit(); // we'll have to define the form to submit
});
Single
If you submit all the forms as one, you'll have to encase them all in a single form, as sending different errors. This could be achieved by using _, and handled in the backend by looping through the different params, saving each one individually.
I'd do this:
#app/controllers/application_controller.rb
def submit
types = %w(schedules exercises workouts)
for type in types do
type.constantize.update_attributes()
end
end
This allows you to create a form with the different data types submitted in the same action:
#app/views/profile/edit.html.erb
<%= form_tag profile_submit_path do %>
<%= fields_for #schedules do |f| %>
<%= f.text_field :title %>
<% end %>
# -> fields_for for the other objects
<% end %>
This will allow you to send the updated objects to your controller, allowing them to submit
If all of your models (Schedules, Workouts and Exercises) are associated, using fields_for should be a good option.
From the above link:
<%= form_for #person do |person_form| %>
First name: <%= person_form.text_field :first_name %>
Last name : <%= person_form.text_field :last_name %>
<%= fields_for :permission, #person.permission do |permission_fields| %>
Admin? : <%= permission_fields.check_box :admin %>
<% end %>
<%= f.submit %>
<% end %>
Read the guides.
You could have some simple javascript that iterates over all form tags and submits each of them.
Alternatively, if you are going to use javascript anyways, you could follow an AJAXish auto-save approach upon changing any field.
But I think it might be cleaner if you just had one form for multiple models, using fields_for.
Is it possible to access another models attributes without having associations? For example I want to create a Prediciton record via a form using the fixture models attributes
<%= form_for #prediction do |f| %>
<%= f.fields_for :fixtures, #fixtures do |builder| %>
<%= builder.text_field :home_team %> VS <%= builder.text_field :away_team %><%= f.text_field :home_score %><%= f.text_field :away_score %><br>
<% end %>
<% end %>
how would i get the attributes of the fixture model without associating the two models?
Thanks
It's much easier if you create the association. If you are not going to create the association, such in the case where you are using a view not backed by a model and your are pulling in and modifying various models from it (assumption I am making) you can do something similar to this:
First make sure you setup routes.rb for whichever methods you are planning to use against the various models.
predictions model
#fixtures = Fixture.all
or specific attributes example
#fixtures = Fixture.select([:home_team, :away_team, :home_score, :away_score]).all
The above is if you are updating another models records. You will also need to modify the create method.
Your view you would want to change from a form_for to a form_tag:
form_tag('/predictions') do
Hopefully this gets you going in the right direction.