【问题标题】:How can I render my defined model in my view?如何在我的视图中渲染我定义的模型?
【发布时间】:2015-07-12 17:24:47
【问题描述】:

不确定我是否以正确的方式进行此操作,但我的模型中有以下内容,并希望在我的视图中呈现它(即显示剩余的开放学生空间的数量)

型号:

   def open_student_spots
              event.student_rsvp_limit - event.student_rsvps_count
   end

这是我试图在我的视图中显示的区域,这是我所做的(尽管到目前为止不正确)

     <i class='fa fa-user'></i>Spaces Available: <%= 
    <% if event.students_at_limit? %>
      (<%= event.student_waitlist_rsvps_count %> Waitlisted)
      <% else %>
     <%  open_student_spots %>  #how to fix this line?
  </div>
<% end %>

请问我该怎么做?

【问题讨论】:

  • 我觉得这是错误的event.student_rsvp_limit - event.student_rsvps_count

标签: ruby-on-rails ruby-on-rails-4 model-view-controller


【解决方案1】:

应该是:

<%= event.open_student_spots %>

1) 使用 = 表示输出。

2) 它是模型上的一个方法,所以你应该在事件实例上调用它。 (它本身可能应该是一个实例变量而不是一个局部变量)

【讨论】:

    【解决方案2】:

    你应该给它像&lt;%= event.open_student_spots %&gt;这样在视图中显示。

    小记:

    <% %> # Executes the code.
    
    <%= %> # Prints the output.
    

    【讨论】:

    • 谢谢 - 这工作。以后我会记住那张纸条!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多