3

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.

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Tried with the Javascript. Does not work, was looking for a solution here on SO and people said that the first solution would only submit the last form. Btw, I had to use $().each(..), because all the forms. My main problem it is because the fields are inside tables, for starts I have one checkbox for 6 schedules(all different from each other), ergo, 6 forms. Gonna try others solutions. Thanks for the answer though.
4

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.

2 Comments

Tried that also. I clicked on the submit button and nothing happened. One Student have many schedules, has many workouts where each workout have many exercises. Thanks for the answer. I'm trying with javascript because of the complexity of my view.
Didn't solve my case. But that would be the easy way to do it. Instead of forms I will only use AJAX call, suits better for me.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.