1

我正在为部分视图编写一些 Rails 代码,并且我希望它仅在有人已经登录到此处的站点时才显示评论字段。

如果页面被还不是站点成员的人查看,则shared/comment_not_logged_in应该传入片段。

但是,我完全不知道为什么我不能运行相同的检查来决定页面是否应该在此处将类属性“missing_your_voice”添加到封闭的 div 元素中:

   <li class="user_submission_form bubble comment_form <% "missing_your_voice" if not current_user %>">

      <% if current_user %>      
        <%= image_tag(current_user.avatar(:comment), :class => "profile_pic") %>
        <% form_for [parent, Comment.new] do |f| %>
        <%= render "comments/form", :f => f %>
        <% end %>
      <% else %>
        <%= render :partial => 'shared/comment_not_logged_in' %>
      <% end %>

  </li>  

同样的成语,"missing_your_voice" if not current_user在 irb 中返回字符串,也在控制台调试器中返回。

我在这里做错了什么?

4

2 回答 2

2

你忘了一个=. 替换<%<%=,得到:

<%= "missing_your_voice" if not current_user %>

请记住,它<% ... %>只会运行 Ruby 代码,但不会显示任何内容。Using<%= ... %>将运行代码显示表达式的结果。

于 2009-06-28T19:48:31.747 回答
0

正如 molf 已经指出的那样,您认为缺少 = 。
应该是<%=

除此之外,请确保通过在您的控制器中调用helper_method使您的控制器方法可用于您的视图。

如果需要,请查看文档。

于 2009-06-28T20:01:10.723 回答