Error saving marked data in a query

Asked

Viewed 70 times

4

I’m creating a Survey that will save the vehicles. For this I have to make a form divided into several pages but when I change the page the data marked on 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 content of the inquiry before directing to the page vehicles

1 answer

2


From what I understood you during the creation of a quiz you’d like to add vehicles to him. It would be quite easy if you could create the quiz and then associate the vehicles, but from what I’ve seen it’s 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 the action new of Questionnaire you must persist the object and already redirect it to the editing area (I don’t know if that’s how you’re doing it, but it’s the first step)

Solution 1

To make this persistent redirection you will have to make the Ubmit of this information to Quesqionnaire and pass on information so that he knows that he will have to redirect, for example

In his Button:

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

For that route I nestled the routes of Vehicles in Questionnaire in the routes.rb in addition to making the appropriate changes in views, but this is another problem.

  resources :questionnaires do
    resources :vehicles
  end

And in his 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’s no way to test the code to see if there’s a mistake, but I think the mechanics might be.

Solution 2

You can use a nested form (Nested Form)

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 created a crud with some of the data submitted and other data deducted for completion the example.

-

PS 2: I saw that you use Gem nested_form she can leave this addition of new vehicles more dynamic (you should already know this, probably was the reason to add it to the project, but it doesn’t cost talk nothing)

Solution 3

You can use Modals to load the form Vehicles and submit it by associating to the @questionnaire, not forgetting to update the list of Vehicles displayed in the form of @questionnaire

Browser other questions tagged

You are not signed in. Login or sign up in order to post.