2

I'm working on a rails scaffold and I'm recording Working Hours in the database.

This is what I did in the controller:

[0,1,2,3,4,5,6].each do |day|
      @biz.hours.build :day => day
    end

I have defined the array in the application model:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 
'Friday', 'Saturday']
end

The controller is working as it should for recording the day index in the database, but I'm having problems in the view. This is my view:

<%= f.fields_for :hours do |builder| %> 
            <tbody>
                <td class="oraritabtit"> <%= builder.label :day, ApplicationRecord::DAY_NAMES[day] %> <%= 
builder.hidden_field :day %> </td>
                <td class="oraritabtit"> <%= builder.time_select :open_time, 
:minute_step => 15 %> </td>
                <td class="oraritabtit"> <%= builder.time_select 
:close_time, :minute_step => 15 %> </td>
            </tbody>
           <% end %>

In the first column, it should show the Week days, but I'm getting this error:

undefined local variable or method `day' for #<#<Class:0x0000000007ca7068>:0x000000000d9517f0>

The problem is with the variable 'day' inside the array's brackets: ApplicationRecord::DAY_NAMES[day]

If I try to substitute the variable in the brackets with 0 for example, I'm getting Sunday on each row of the table, so the array is properly working.

Raffaele
  • 21
  • 2

0 Answers0