Rails从不同的视图控制器访问实例变量

时间:2017-01-30 04:04:10

标签: ruby-on-rails ruby heroku

我的应用程序出现问题,而且我已经用尽所有尝试来获得解决方案。我的问题是:我有一个控制器,其中有一个名为composition的实例变量,我的静态页面有另一个控制器,我想将组合变量传递给我的索引。我该怎么办?

Heroku错误

2017-01-30T03:37:03.434514+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5] ActionView::Template::Error (undefined method `each' for nil:NilClass):
2017-01-30T03:37:03.435598+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     16: 
2017-01-30T03:37:03.435601+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     17: <div class="panel-container">
2017-01-30T03:37:03.435602+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     18: 
2017-01-30T03:37:03.435603+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     19:   <% @compositions.each do |composition| %>
2017-01-30T03:37:03.435604+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     20:       <div class="panel player">
2017-01-30T03:37:03.435604+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     21:         <div class="panel-top player-top">
2017-01-30T03:37:03.435605+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]     22:           <div class="row">
2017-01-30T03:37:03.435633+00:00 app[web.1]: [14a3c747-4c38-4392-b5d4-63bc0379b7e5]   

compositions_controller.rb

def index
  @compositions = Composition.all.order('created_at desc')
end

static_pages / index.html.erb

<div class="panel-container">
<% @compositions.limit(4).each do |composition| %>
  <div class="panel player">
    <div class="panel-top player-top">
      <div class="row">
        <p class="panel-title"><%= composition.title %></p><span class="icon-dots-three-horizontal"></span>
      </div>
      <div class="row user-details">
        <p>Projeto por <%= composition.user.username %></p><a href="#" class="ui-btn btn-small"><span class="icon-export"></span>visitar
        perfil</a>
      </div>
    </div>
    <div class="panel-bottom player-controls">
      <div class="row">
        <div class="col30">
          <p><span class="icon-controller-play"></span><%= composition.plays %></p>
          <p><span class="icon-note"></span>230</p>
        </div>
        <div class="col30"><span class="controllers"><span class="icon-controller-play"></span>
            <p>2:15 / 430</p></span></div>
        <div class="col30">
          <p><span class="icon-chat"></span>23</p>
        </div>
      </div>
    </div>
  </div>
  

1 个答案:

答案 0 :(得分:1)

只需在@compositions

中再次定义static_pages_controller#index即可