【问题标题】:Ruby on Rails passing parameters to partialsRuby on Rails 将参数传递给局部变量
【发布时间】:2016-01-06 19:13:36
【问题描述】:

这是我的 .erb 文件;

<% if @comment.reviewer_notes && current_user.type == "User::Reviewer"%>
      <% @comment.reviewer_notes.each do |reviewer_note| %>
        <?= reviewer_note.rating %> <-- this "works" ie prints out rating on screen
        <%= render 'partials/comments/form' %>

这是我的 .erb 部分;

<div class="admin-rating stars clear">
  <%= reviewer_note.rating %> <-- this does not work. 
</div>

我得到的错误是;

#TipComment:0x007f8b38e29328 的未定义方法“reviewer_rating”>

如何传入reviewer_note.rating

【问题讨论】:

  • 除此之外,我会将current_user.type == ... 更改为 user.reviewer?并在用户模型中添加实例方法。
  • 了解locals

标签: ruby-on-rails partials


【解决方案1】:
<%= render partial: 'reviewer_note_rating', locals: {reviewer_note: reviewer_note} %>

【讨论】:

    【解决方案2】:

    您应该将它作为变量传递给 Partial。

    &lt;%= render 'partials/comments/form', note_rating: reviewer_note.rating %&gt;

    【讨论】:

      猜你喜欢
      • 2013-07-05
      • 2015-12-10
      • 2012-08-28
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多