Rails Single Form with Multiple Instances of A Model

I wanted to have a single form in my Rails app that submitted and created multiple instances of a Model. Seems like this would be a relatively common thing to do, right? But I found it surprisingly difficult to find good examples that I could follow. I worked on this for a long time, and hopefully this will save you some time.

I originally tried the approach of simply having multiple identical forms on the same page, and using Javascript to recursively submit them (hidden to the user). I imagine this could work, but I had difficulty getting it working, and realized that this approach had problems: 1) It is very slow and inefficient and 2) its not scalable. And I felt “there has to be a better way”.

I tried to use the Surveyor gem but it is no longer in development, and I couldn’t get it working with the latest Ruby and Rails 5. Toward the end of my work I was told about the Reform gem, which looks like it could work, but I had already gotten things figured out so I haven’t tried that gem.

So, on to the specifics of the problem. I wanted to create an ‘exam’ for users, that would be on a single web page. They would answer several questions and get a score. The way I have this set up is with a Question, Response, and Answer models. The Answer model stores the correct answers, while the Response model stores a user’s responses. Now if anyone knows a better way to structure this, I’d love to know, but its what I’ve come up with so far.

class Question < ApplicationRecord
	# Associations
	has_many :answers
end

class Answer < ApplicationRecord
	# Associations
	belongs_to :question
end

class Response < ApplicationRecord
	# Associations
	belongs_to :user
	belongs_to :question
end

Ok, so on the exam page, I want to display all of the Questions, and provide inputs so the user can submit all of their Responses, one for each Question. I want to do this on one webpage, and have one form that submits all of the user’s Responses. There are multiple ways to approach this, and I found a very helpful list (this gist) from Ryan Bates (of Railscasts) and discussed in the Rails Google Group thread.

The way I chose to approach it, I wanted to get the params to look like this:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"", 
"responses"=>
  {
    "0"=>{"response_value"=>"a", "user_id"=>"23", "question_number"=>"1"},
    "1"=>{"response_value"=>"b", "user_id"=>"23", "question_number"=>"2"},
    "2"=>{"response_value"=>"c", "user_id"=>"23", "question_number"=>"3"}
  },
 "commit"=>"Submit Exam", "method"=>"post"}

This is similar to ‘approach 4’ in the Ryan Bates gist referenced above. Essentially, I wanted a single form to submit params where the multiple response objects are in a hash in the params, where they are ordered. They are referenced in the hash by a plural of the model, responses.

So for example, the first response is under {"responses"=>{"0"}}

{"response_value"=>"a", "user_id"=>"23", "question_number"=>"1"}

I didn’t know how to do this, but after a lot of reverse engineering, here’s what I learned. In order to make this work when the HTML form is submitted, we need the name value of each input to be correct and unique. The name attribute of an html input is what links it to how it is defined in the params. So we need our name attribute to be as such in the rendered HTML:

input id="responses_response_value_a" class="radio" 
name="responses[0][response_value]" type="radio" value="a"

So for each Question in the exam, the Response will correspond to a unique numbered value in the params hash. In the above example, it is the 0 in responses[0][response_value]. So for example, the HTML input for response number 3 is:

input id="responses_response_value_c" class="radio" 
name="responses[2][response_value]" type="radio" value="c"

Keep in mind that I’m numbering these starting from 0, as in indices of an array. But you could use any unique value. The reason for this is to separate them in the hash, so then in our controller we can iterate over the params hash, calling create on each response in the params (which we will get to below). I chose to make the unique identifiers simple integers starting from zero to make the iteration code in the controller easier, as you will see below.

Ok, so thats the HTML that we want to generate. Of course we could manually create that in our Rails view file in straight HTML. But that wouldn’t be very “Railsy”. So how do we create that in our erb view file in Rails? Well, we can use Rails methods…

But first, in our controller for the Exam page, we need to create an array of the multiple instances of the Responses model that we are going to create when we submit the form. We do this before we render the exam page. So in whichever controller handles the page where we have our exam, in my case its called pages_controller.rb, I want to create an empty Response for each Question in the exam. I do this be creating an array of empty response objects:

def exam_page
    @empty_responses = []

    Question.all.each do
        @empty_responses << Response.new
    end
end

Great, so now we have an array of empty Responses, one for each Question. Now in our view, we can iterate over that array, and create form inputs for each Response, corresponding to the values for all those empty responses, which will get saved when we submit the form. So in exam_page.html.erb


form_tag responses_path(method: :post) do
   @empty_responses.each do |response|
     fields_for 'responses[]', response, include_id: false do |r|
      # grab each question and display its text
      q = Question.where(question_number:                    
                @empty_responses.index(response) + 1).first
      q.question_text
      r.collection_radio_buttons(:response_value,               
            q.answers.order("answer_text"), :answer_text, :answer_text, 
            include_hidden: false) do |r|                 
               r.radio_button(class: 'radio' + 
               @empty_responses.index(response).to_s, name:
               'responses[' + (q.question_number - 1).to_s +   
               '][response_value]') + r.label(:class => 'radio-label')
             end
      r.hidden_field :user_id, value: current_user.id,
             name: 'responses[' + @empty_responses.index(response).to_s
              + '][user_id]'
      r.hidden_field :question_number, value: q.question_number,
             name: 'responses[' + @empty_responses.index(response).to_s  
            + '][question_number]'
    end 
  end
  submit_tag 'Submit Exam', class: 'btn btn-primary btn-xl'
end

(Note that I removed the erb tags because I had trouble getting WordPress to display them correctly)
Ok, a lot going on here, lets break it down. So we use the form_tag for a new Response. I use the form_tag instead of form_for, as form_forseemed to be more difficult to get to handle multiple Responses, as it typically only accepts one. form_tag seemed to be more vanilla HTML.

In the next line we iterate over our array of empty responses that we created in the controller. Next we use fields_for to tell Rails that we are creating inputs for something different than a simple direct ‘one level deep’ Response. Remember we are going ‘two levels deep’ in our params, to include multiple Responses in our params hash. As this is not the default behavior, using fields_for allows us to tell Rails that we are creating input values for something other than what the form is for in the first line with the form_tag. Of course fields_for is often used if you want to include input values for a different Model, but in our case we are co-opting it to help us submit multiple instances of the same Model. We use "responses[]" to get us to the ‘second level’ in the params hash. So this means that any input values that come after our fields_for "responses[]" will correspond in the params hash to {responses=>{}}.

And thats how our name value on the HTML input becomes "responses[X][response_value]". Make sense?

So that’s the basics. However, since in my case I’m creating multiple choice questions with radio buttons, it ads some extra complications (of course!). I use the Rails form helper collection_radio_buttons. Lets go through each of these parameters as this was quite confusing to me initially. :response_value is the field in the database of the Response model that we want this data to be saved to. q.answers.order("answer_text") is the list of Answers, one for each radio button. The first :answer_text is the value that we want the radio button to be associated with, and the second :answer_text is the text that we want to display next to each radio button. Theinclude_hidden: false is an option that we pass in, which I’ll discuss below. Whew!

Here again, the name value is critical for us to get the HTML code right so that the Response gets put correctly into the params when submitted to the server. So here we specify the name attribute for each radio button. We need the iterate through and create the indices to be used in our params hash, as discussed above. Thats why I use the code: name: 'responses[' + (q.question_number - 1).to_s + '][response_value]'. So this gives us the "responses[X][response_value]" that we wanted in the HTML. The -1 is because I numbered my questions starting at 1, but I wanted to number the responses in the hash starting at 0. Now its important to note that for radio_buttons, the group of radio_buttons that are grouped together as a single set of choices must have the same name attribute.

The id value in the HTML is also important, as you can see it also links us to 2 levels deep by saying responses_response_value_c where ‘c’ is the value that is going to be submitted by choosing that input. This is automagically created by the rails helpers, and I found I didn’t need to alter it.

Here’s what the ultimate HTML looks like for the radio button options for the Response to the first Question:

input class="radio0" name="responses[0][response_value]" type="radio" 
      value="a" id="responses_response_value_a"
label class="radio-label" for="responses_response_value_a">"a"
input class="radio0" name="responses[0][response_value]" type="radio" 
      value="b" id="responses_response_value_b"
label class="radio-label" for="responses_response_value_b">"b"
input class="radio0" name="responses[0][response_value]" type="radio"
      value="c" id="responses_response_value_c"
label class="radio-label" for="responses_response_value_c">"c"
input class="radio0" name="responses[0][response_value]" type="radio"
      value="d" id="responses_response_value_d"
label class="radio-label" for="responses_response_value_d">"d"

And here’s what my params look like in Terminal after submitting the form with multiple responses (3 Responses to 3 Questions):

Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", 
"responses"=> {
     "0"=>{"response_value"=>"a", "user_id"=>"23", "question_number"=>"1"},
     "1"=>{"response_value"=>"b", "user_id"=>"23", "question_number"=>"2"},
     "2"=>{"response_value"=>"c", "user_id"=>"23", "question_number"=>"3"}},
"commit"=>"Submit Exam", "method"=>"post"}

Great! Now some additional nuances. As we mentioned above, on the fields_for call, we pass in include_id: false. This is because, according to the fields_for documentation, it automatically generates a hidden field to store the ID. However, in our case this is interfering with our Responses and the name values linking to the params. To turn this off, we pass in include_id: false to prevent fields_for from rendering it automatically.

A similar thing happens in the collection_radio_buttons code; you see I pass in the include_hidden: false value. This is because Rails 5 by default now creates a hidden value that is blank so that even if the user submits the form without clicking on any radio button, a blank value will be sent to the server (default HTML doesn’t do that). I learned this from this blog post by Prajakta Tambe, which explains this very well and includes other links to the documentation. Since I’m customizing all of these inputs, I needed to turn off that default behavior.

Whew! That was a lot of info. Hopefully it sort of makes sense to you? This took me forever to understand and figure out, and hopefully it saves you time.

Update: Someone asked about how to access these params in the controller, so I’m adding the controller code below. In the create function, basically just iterating through each of the “responses” in the params, and creating a new instance of the Response model for each, passing in the parameters that were sent in the params hash.

def create
  params["responses"].each do |key, value|
    Response.create(response_params(value))
  end
end

Also, as explained in this Vic Friedman post, we have to adjust the strong params function to handle submitting the params in the altered structure in the create function.

So in the bottom of the controller, our new params function now accepts an argument, which is our new params structure:

def response_params(my_params)
  my_params.permit(:question_number, :answer_id, :user_id, :correct,       :response_value)
end

So that’s what I put in the controller code.

credits:

Vic Friedman

Ryan Bates Gist

NestedAssignment

Rails Documentation for Building Complex Forms

Railsguides – Simple Form Array Text Input

Ryan Bates Advanced Rails Recipes

StackOverflow Question 1

StackOverflow Question 2

Using AJAX and jQuery in Rails 5

RichOnRails – AJAX in Rails

LaunchSchool

Embed ERB Ruby Code in CSS in Rails

Did you know that you can embed erb code into CSS in rails? I just learned it, and its awesome, and simple.

Start by adding the .erb extension to your css file:

pages.css.erb

Then bam! you can put erb in your css code:

.navbar-brand {
background-image: url(<%= asset_path 'new-logo.png' %>);
background-size: contain;
background-repeat: no-repeat;
}

Initialize Rails App Databases Using MySQL After Cloning Repository From Github

So I cloned my colleague’s rails app from github and wanted to run it on my machine so I can collaborate on the code.  But I kept getting the error that the database didn’t exist.

ActiveRecord::NoDatabaseError (Unknown database 'owl_development')

First, I checked what database this rails app was using by looking at the database.yml file and found it was using mysql and the mysql2 gem.

So I made sure that I had mysql installed on my system by using homebrew and the brew install mysql command.  Then I tried rake db:migrate and still got the error that the database didn’t exist.

What I had forgotten to do after I cloned the repo was run

rake db:create

to create the databases.  I know this is obvious in hindsight, but I spent like 3 hours searching around until I figured out that’s what I had forgotten.  Hopefully this will save you some time!

partial credit here

Validate Active Model Numbers Greater Than and Less Than

If you have a model and you want to do validation related to numerical values, such as validate a certain number is less than or greater than another, here’s how you can do it.

validates :inventory_qty, :numericality => { greater_than_or_equal_to: 0, less_than: 100 }

Checkout the documentation for more details.

Credit here.

Use jQuery to Animate Background Color

Although CSS keyframes is probably the easiest way to animate the background color of an element on the page, since earlier versions of Internet Explorer (IE < 10) don’t support CSS keyframes, if you need a more widely supported solution, you can do the same thing in jQuery.

To do this, you use the jQuery .animate() function.  The catch is that in order to be able to change colors with this function, you also need to include the jQuery Color plugin.

The way I did this in Rails was to just create a new file under assets/javascripts and called it jquery_color.js. Then copied the code from the plugin into that file.

Ok, now for the actual code. Credit for this goes to StackOverflow user fitorec‘s answer to this question, and this jsfiddle that he wrote.

$(document).ready(function() {
  var animate_loop, colors, i;
  colors = ['#FFB30C', '#58EC00', '#0087EC', '#EEEEEE', '#FF5A00'];
  i = 0;
  animate_loop = function() {
    $('body').animate({
      backgroundColor: colors[i++ % colors.length]
    }, 1500, function() {
      animate_loop();
    });
  };
  animate_loop();
});

This creates a colors array with the colors that you want to animate. Then it calls the .animate() function, passing in the backgroundColor property, and assigning it to one of the colors in the colors array, and iterates i to go through the array. The animate_loop function then calls itself, to create an infinite loop.

Of course if using Rails you can use js2coffee to convert to coffeescript.

Rails Form form_for Objects Reverse Order of Array

This is pretty simple, but I couldn’t figure out how to do it.  Basically I had a view that went through each object in the nested table array, and rendered it.  However, instead of rendering them all in the order they were created from top to bottom on the page, I wanted to render them in reverse order, so the most recent one was at the top of the page.  There might be several ways of doing this, but here is how I finally did it, and its pretty simple. (probably shouldn’t have taken me this long to figure it out, but hopefully you can read this and save some time!)

Here is the original code in the view:

  
<%= simple_form_for(@lot) do |f| %>
  <strong>Lot Number:</strong> <%= @lot.number %>
  <strong>Item</strong> <%= Item.find(@lot.item_id).number_rev_name %>
  <strong>Inventory Quantity:</strong> <%= @lot.inventory_qty %>

  <-- Here is where I render a partial for each build_lot, which is a 
         has_many, :through join_table -->
  <%= f.simple_fields_for :build_lots,  do |build_lot| %>
    <%= render 'build_lot_fields', :f => build_lot %>
  <% end %>
  <%= f.button :submit, class: 'btn btn-primary' %>
<% end %>

And here is the code from the partial for each build_lot, _build_lot_fields.html.erb

<div class='nested-fields build-lot-fields'>
  <div class='well'>
    <%= f.input :my_build_id, collection: MyBuild.order(:start_date), label_method: :description, value_method: :id, :prompt => 'Choose a Build', selected: MyBuild.where(current: true), input_html: { id: 'build_select'} %>
    <%= f.input :pull_quantity, label: 'Pull Quantity', as: :string %>
    <%= f.label 'Date' %><%= Date.today %><br>
    <%= f.input :employee_id, collection: Employee.order(:first_name), label_method: :first_name, value_method: :id, prompt: "Choose your name" %>
  </div>
</div>

So this simple trick to reverse the order of the build_lots array that is iterated through to render each partial, is to simply add another parameter to the fields_for call (in this case simple_fields_for from the simple_form gem). This second parameter that we add will be the build_lots array reversed. You can find more about the usage of the fields_for method on its documentation page.

But first we need to create this reversed array in the controller. So in the controller (in the function for the view where I want to show the reversed build_lots) I added this line:

@reverseBuildLots = @lot.build_lots.reverse

Then back in the view, I simply change the form_for line like this:

<%= f.simple_fields_for :build_lots, @reverseBuildLots do |build_lot| %>

passing in the @reverseBuildLots parameter, which is an array of build_lots in reverse order that we created in the controller.

Pretty simple, but took me a while to figure out. Hopefully this will save you some time!

credit here

Customize Devise Timeoutable Length of Time Until Auto Sign Out

The Devise gem is awesome and super useful if you have users logging into your app.  It has a built-in feature for making user sessions automatically signed out or logged out after a certain period of time. Devise calls this :timeoutable.  You can easily customize how long until a user is signed out by going to the config/initializers/devise.rb file and editing the config.timeout_in = 30.minutes line.

  # ==> Configuration for :timeoutable
  # The time you want to timeout the user session without activity. After this
  # time the user will be asked for credentials again. Default is 30 minutes.
  config.timeout_in = 30.minutes # EDIT THIS LINE

So in mine I changed it to

config.timeout_in = 5.minutes

while testing.

Let me know if this was helpful!