0

I created a array in the controller to get the values of another database as

@exam_group = ExamGroup.find(params[:exam_group])
@student = Student.find_by_id(params[:student])
@batch = @student.batch
@modules = StudentAdditionalField.find(:all)
@total = Answer.sum(:marks)
@exams = []
@modules.each do |mod|
  @exams.push mod.name unless mod.name.nil?
  @exams.push mod.marks unless mod.marks.nil?
  @exams.push total unless total.nil?
end

The databse of @modules is

+----+--------+-------+-------+
| id | name   | total | marks |
+----+--------+-------+-------+
|  1 | Quants |     5 |     5 |
+----+--------+-------+-------+

It should be displayed in view as

<table id="listing" align="center" width="100%" cellpadding="1" cellspacing="1">
  <tr class="tr-head">
    <td><%#= t('subject') %></td>
      <td><%= t('marks_obtained') %></td>
      <td><%= t('max') %></td>
    </td>
  </tr>
  <% @exams.each do |es| %>
    <tr class="tr-<%= cycle('odd', 'even') %>">
      <td class="col-1"> <%= es.name %></td>
        <td class="col-1"><%= es.marks || '-' %></td>
        <% total_marks_attained = @total %>
        <td class="col-1"><%= es.marks %></td>
        <% total_max_marks = @total %>
      </td>
    </tr>
  <% end %>

    <tr class="tr-head">
      <td><%= t('total_marks') %>:</td>
      <td><%= total_marks_attained %></td><td><%= total_max_marks %></td>

    </tr>

</table>

But it returns error as undefined method 'Quants' for "Quants":String.

Please help me, thanks in advance.

2 Answers 2

1

Your @exams array looks like: ['quants',5,5,'not_quants',4,3,...] after running code, and you are trying to get marks and name from every element in this array. So, you have no methods marks and name for string and ints, and you are getting error. I'm not sure why it returns error about method `Quants', but definitely you have error here.

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

Comments

0

In exams you have pushed some thing like that mod.name and while using it in view you are again doing es.name which is causing this error. Since es is already 'Quants' and you cant call a method on that.

You can use @module directly in view and give those conditions there or make @exams an array of objects instead of modules's name,total and marks

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.