如何使对象的属性显示在视图中

时间:2014-12-02 11:56:05

标签: ruby-on-rails

我在Rails中使用MVC。我有一个非常愚蠢的问题,但我无法让它工作....我有一个模型对象控件,而控件有三个属性(level,day => DateTime,period)。这些时期是这些时期:@@期间= ['早餐前','早餐后','午餐前'' post-午餐''下午''餐前''晚餐后'。我的问题是,当我创建一个新控件并将其打印在我的页面上时,它没有显示的时间段,而是从1到7的数字。这是我的代码

视图的代码

控制

<ul>
<% @controls.each do |control| %>
    <li><%= "Blood Sugar Control:" + control.level.to_s + " - " + control.period.to_s + " - " + control.day.to_s %></li>
    <%= link_to "Delete", control_path(control.id), method:'delete', data: {confirm: "Are you sure?"} %>
    <%= link_to "edit", edit_control_path(control)%>
</ul>
<% end %>
</div>

这是控制器的代码

def months
    @user= current_user
    @controls= @user.controls.order_by_date
end

这是表格的代码

<%= form_for @control, :html => {:class => 'form-inline'} do |f| %>
    <p class="new_control_text">New Control</p>
      <div class="form-group">
        <div class="input-group">
          <%= f.label :level, class: "sr-only", for: "exampleInputEmail2" %>

          <%= f.number_field :level, class: "form-control", id: "exampleInputEmail2", placeholder: "Enter level" %>

        </div>
      </div>

      <div class="form-group container">
        <div class="row datetimeselect">
            <div class='col-sm-12'>
                <div class="form-group">
                    <div class='input-group date' id='datetimepicker1'>

                        <%= f.text_field :day, default: DateTime.current, :class => 'form-control' %>
                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
         </div>
     </div>

    <div class="select_period">
       <%= f.select :period, options_for_select([['pre-breakfast'], ['post-breakfast'],['pre-lunch'],['post-lunch'],['afternoon'],['pre-dinner'],['post-dinner']]), {}, class: "form-control" %>
    </div>      

      <%= f.submit "Save", type:"submit", class: "btn btn-default button-save" %>

<% end %>

这是模型的代码

def get_day
    calendar_day = DateTime.new(day.year, day.month, day.day)
    calendar_day
end

def self.order_by_date
    @controls = Control.all.sort_by { |control| [control.get_day, control.period]}
end

我希望你能帮助我理解为什么它没有显示出来的控制时间。谢谢

0 个答案:

没有答案