Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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:

  1. Every form you submit will go to the form's own update method in the backend
  2. 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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top