Rails - formatting a list of options

90 Views Asked by At

I'm trying to make an app with rails 4.

In my show page, I want to show the output of choices made by a user who is offering participation in a project. There are several options and a user might choose one or more of them. I have the output formatted like a sentence, so that it reads properly if only one choice of output is made. Sometimes, more than one choice will be made and I'd like them to be displayed in grammatically correct format (so with commas separating choices and 'and' between the last and second last choice).

My format is currently:

<div class="datageneraltext">Participants receive

<span>

        <% if @project.scope.try(:result).try(:report) == true  %>

            <%= render :text => "a written project report" %>

        <% end %>



        <% if @project.scope.try(:result).try(:standard_licence) == true  %>

            <%= render :text => "a licence to use the project results (a standard licence)" %>

        <% end %>



        <% if @project.scope.try(:result).try(:bespoke_licence) == true  %>

          <%= render :text => "a licence to use the project results (a bespoke licence)" %>

    <% end %>




    <% if @project.scope.try(:result).try(:option) == true  %>

      <%= render :text => "an option to acquire rights to use the the project results" %>

    <% end %>




    <% if @project.scope.try(:result).try(:assignment) == true  %>

      <%= render :text => "an assignment of rights to commercialise the project results" %>

    <% end %>




    <% if @project.scope.try(:result).try(:consulting) == true  %>

      <%= render :text => "engagement for further research consultation" %>

  <% end %>



    <% if @project.scope.try(:result).try(:other) == true  %>

      <%= @project.scope.try(:result).other_outcome %>

  <% end %>

</span>


  </div>
<div class="datageneraltext">

  <%= @project.scope.try(:result).description %>

  </div>

I have a form which users use to select the results as follows:

  <div class="col-md-7">
      <div class="response-project" style="margin-left:5%">
        <%= f.simple_fields_for :scope do |results_s| %>
            <%= results_s.simple_fields_for :result do |r| %>
                <%= r.input :report, :as => :boolean, :label => false, inline_label: "A report on project outcomes" %>
                <%= r.input :standard_licence, :as => :boolean, :label => false, inline_label: "A standard licence to use project results" %>
                <%= r.input :bespoke_licence, :as => :boolean, :label => false, inline_label: "A bespoke licence to use project results" %>
                <%= r.input :option, :as => :boolean, :label => false, inline_label: "An option to acquire an interest in the project outcomes" %>
                <%= r.input :assignment, :as => :boolean, :label => false, inline_label: "Assignment of project outcomes" %>
                <%= r.input :consulting, :as => :boolean, :label => false, inline_label: "Available to consult in relation to the outcomes" %>
                <%= r.input :other, :as => :boolean, :label => false, inline_label: "Another outcome (not listed above)" %>

Does anyone know how to write a loop through these so that if there is only one choice, it remains as I have it, if there are two choices, they are separated by the word 'and' and if there are more than two choices, commas separate all but the last two choices, which are separated by the word 'and'?

Thank you

1

There are 1 best solutions below

1
On

It would help to see how your app is modelled. Somewhere you'll need to store the desired text to be output, then you can use a helper method to grab the appropriate response and format it. A rudimentary solution would be a hash in the project model which could be used in a helper method. The below will give you some ideas to work with.

class Project
  def response_text
     {  report: "a written project report",
        standard_license: "a licence to use the project results (a standard licence)"
     }
  end
end

in the partial

<%= format_response(@project) %>

helpers/project_helper.rb

def format_response(project)
   output = []
   project.scope.result.each do |option|
     output << response_text[option] if option
   end
   output.join(', ')
   content_tag(:span, output)
end