Error saving the data marked in a questionnaire

4

I'm creating a survey that will save vehicles. For this I have to make a form divided into several pages but when I change the page the data marked in the radio buttons are lost

Controller

def new_vehicles
   @vehicle = Vehicle.new
   @vehicle.questionnaire = @questionnaire
   @vehicle_types = VehicleType.all      
   render "vehicles/_form"
end

View

<%= nested_form_for(@questionnaire) do |f| %>
  <%= f.label :question_2_3, "2.3 A estrada de acesso ao domicílio é transitável no tempo de chuva?" %><br>
  <%= f.radio_button :question_2_3 ,"1" %><label >Sempre</label>
  <%= f.radio_button :question_2_3 ,"2" %><label >Não</label>
  <%= f.radio_button :question_2_3 ,"3" %><label >Mais de 50% do tempo das chuvas</label>
  <div class="radioholder clear">
    <label>2.1 Nos últimos 12 meses, anote quantos veículos foram de propriedade dos moradores da casa?</label><br>
    <%= button_to "Inserir +", new_vehicles_questionnaire_path(@questionnaire), remote: true %>
  </div>
  <%= f.submit "Salvar" %>
<% end  %>

Save the questionnaire content before going to the vehicles page

    
asked by anonymous 28.06.2016 / 20:56

1 answer

2

As I understood you during the creation of a questionnaire you would like to add vehicles to it. It would be quite easy if you could create the questionnaire and then associate the vehicles , but from what I saw it is not the case.

I see other possibilities to solve this problem, but judged that you really want to persist the data of radio_button before adding the vehicles we can try to do the following:

In% with% with% of% with% you should persist the object and redirect it to the edit area > Solution 1

To make this persistent redirect you will have to submit this information to Quesqionnaire and pass on information so that it knows you will have to perform the redirect, for example

On your Button :

<%= f.button "Adicionar Veículo", name: "redirect_to_action[#{questionnaire_vehicles_path(@questionnaire)}]" %> 

For this route I've nested the vehicle routes in questionnaire in action and make changes to the views, but that's another problem.

  resources :questionnaires do
    resources :vehicles
  end

And in your controller :

  # (...)     

  def update
    if @questionnaire.update(questionnaire_params)
        redirect_to custom_redirect(params)
        # (...)
    end
  end

  # (...)

  private

    def custom_redirect(params)
      if params[:redirect_to_action] and ! params[:redirect_to_action].keys.empty?
        params[:redirect_to_action].keys.first
      else
        @questionnaire
      end
    end

There is no way to test the code to see if there is an error, but I think the mechanics can be that.

Solution 2

You can use a nested form ( new )

Model Questionnaire

class Questionnaire < ActiveRecord::Base
  has_many :vehicles
  accepts_nested_attributes_for :vehicles
end

Model Vehicle

class Vehicle < ActiveRecord::Base
  belongs_to :questionnaire
end

questionnaires / _form.html.erb

<%= form_for(@questionnaire) do |f| %>
  <div class="field">
    <%= f.label :question_2_3, "2.3 A estrada de acesso ao domicílio é transitável no tempo de chuva?" %><br>
    <%= f.radio_button :question_2_3 ,"1" %><label >Sempre</label>
    <%= f.radio_button :question_2_3 ,"2" %><label >Não</label>
    <%= f.radio_button :question_2_3 ,"3" %><label >Mais de 50% do tempo das chuvas</label>
  </div>
  <%= f.fields_for :vehicles do |f_vehicle| %>
    <div class="field">
      <%= f_vehicle.label :model %><br>
      <%= f_vehicle.text_field :model %>
    </div>
    <div class="field">
      <%= f_vehicle.label :color %><br>
      <%= f_vehicle.text_field :color %>
    </div>
    <div class="field">
      <%= f_vehicle.label :year %><br>
      <%= f_vehicle.number_field :year %>
    </div>
  <% end  %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

QuestionnaireController

# (...)

  def edit
    @questionnaire.vehicles.build
  end

# (...)

  def questionnaire_params
    params.require(:questionnaire).permit(:question_2_3, vehicles_attributes: [:model, :color, :year])
  end
  

PS: As I have no idea of your real structure, I have created crud with   some of the data presented and other data deducted to complete   the example.

-

  

PS 2: I saw that you use the gem Questionnaire it can leave this   adding new routes.rb more dynamic (you should already know this,   was probably the reason for adding it to the project, but it does not cost   nothing to talk about)

Solution 3

You can use Nested Form to load the form of nested_form and submit it by associating with vehicles , not forgetting to give the update in the Modals list displayed in the Vehicles form

    
28.06.2016 / 22:05