【问题标题】:Rails 4 - helper method not responding to if conditionRails 4 - 辅助方法不响应 if 条件
【发布时间】:2016-08-03 01:45:52
【问题描述】:

我正在尝试弄清楚如何在 rails 4 中使用辅助方法。

我有个人资料显示页面 - 如果当前用户正在查看: - 他们自己的个人资料页面,他们应该在该页面上有一个包含 6 个选项的菜单(每个选项都链接到该用户个人资料的可见部分);要不然 - 另一个用户的个人资料页面,他们应该在该页面上有一个包含 4 个选项的菜单(每个选项都链接到该其他用户个人资料的可见位)。

在我的助手文件夹中,我有一个profiles_helper.rb:

module ProfilesHelper

def items_for_profile_menu(profile)
        if current_user.id = @profile.user_id 
      "<li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#006F7F'>
               <a href='index.html' class='hvr-sweep-to-bottom'>
                      <br><br>
                      <span>Dashboard</span>
               </a>
        </li>"
    else
 "<li class='col-xs-6 col-sm-3 nopadding menuitem blue'>
       <a href='resume.html' class='hvr-sweep-to-bottom'>
       <i class='flaticon-graduation61'>
       </i><span>Researh History</span></a>
   </li>"

在我的个人资料显示中,我有:

<ul id="nav" class="row nopadding cd-side-navigation">
   <%= raw (items_for_profile_menu(@profile)) %>   
</ul>

当我保存所有这些并尝试时 - 当我以个人资料所有者身份登录时,我会得到相同的 6 项菜单,就像我从另一个帐户查看个人资料时一样。

我如何获得这个帮助方法来区分当前用户的 id 与显示的个人资料的 profile.user_id 相同的当前用户和当前用户自己的个人资料页面?

我的模特协会是:

用户:

has_one :profile

简介:

belongs_to :user

【问题讨论】:

  • 我不建议将视图代码混合到帮助程序中。简化帮助程序(例如def this_user?)以确定是否应显示输出。然后使用&lt;% if this_user? %&gt;template if logged in&lt;% else %&gt;template if logged out&lt;% end %&gt;
  • @Owen - 如果其他用户也恰好登录了会发生什么?我只想向拥有该个人资料的用户显示完整菜单。
  • 还有 - 你的意思是@profile 还是profile(因为后者作为参数传递给助手)?
  • 欧文 - 我不明白。在显示页面中,我想根据用户是个人资料所有者还是个人资料访问者来显示菜单项。我认为@profile 在这种情况下是正确的,因为我想给它正在访问的个人资料页面。

标签: ruby-on-rails ruby helper


【解决方案1】:

您的问题是使用赋值而不是比较。

if current_user.id = @profile.user_id 

应该阅读

if current_user.id == profile.user_id

但是,在我们研究这一点时,您应该考虑将视图代码与应用程序逻辑分开。如果可能,所有输出 HTML 的代码都应该在视图中。所以你的助手应该被简化,示例如下:

module ProfilesHelper
  def logged_in_as?(profile)
    current_user.id == profile.user_id
  end
end

那么你的个人资料显示可以是:

<ul id="nav" class="row nopadding cd-side-navigation">
  <% if logged_in_as?(@profile) %>
    <li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#006F7F'>
      <a href='index.html' class='hvr-sweep-to-bottom'>
        <br><br>
        <span>Dashboard</span>
      </a>
    </li>
  <% else %>
    <li class='col-xs-6 col-sm-3 nopadding menuitem blue'>
      <a href='resume.html' class='hvr-sweep-to-bottom'>
        <i class='flaticon-graduation61'>
        </i><span>Researh History</span></a>
    </li>
  <% end %>
</ul>

通过这种方式,HTML 全部组合在一起,当您想要更改它时,您无需乱用您的助手 - 此外,您可以在其他地方重复使用它,以便在其他模板中实现简单的逻辑。

【讨论】:

  • 谢谢你 - 这行得通。当计时器允许我接受时,我会接受。
【解决方案2】:

我同意 owen 在这种情况下不需要创建 html 递归。我认为最好不要抽象的观点。

profiles_Helper.rb

module ProfilesHelper

  def is_current_user?(profile)
     current_user == profile.user
  end

end

views/xxx.html.erb

<%- if is_current_user?(@profile) %>
  <li class='col-xs-4 col-sm-2 nopadding menuitem' style='background:#006F7F'>
    <a href='index.html' class='hvr-sweep-to-bottom'>
      <br><br>
      <span>Dashboard</span>
    </a>
  </li>"
<% else %>
  <li class='col-xs-6 col-sm-3 nopadding menuitem blue'>
    <a href='resume.html' class='hvr-sweep-to-bottom'>
      <i class='flaticon-graduation61'>
      </i><span>Researh History</span>
    </a>
 </li>"
<% end %>

【讨论】:

  • 投了这个帖子才看到欧文的回答。
  • 您的代码仍然有意外的赋值运算符。
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 2015-09-03
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多