Rails 4 - 辅助方法不响应 if 条件
Rails 4 - helper method not responding to if condition
我正在尝试弄清楚如何在 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
你的问题是使用赋值而不是比较。
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 就在一起了,当你想改变它的时候你不需要弄乱你的助手 - 另外,你可以在其他地方重用它以在其他模板中实现简单的逻辑。
我同意 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 %>
我正在尝试弄清楚如何在 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
你的问题是使用赋值而不是比较。
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 就在一起了,当你想改变它的时候你不需要弄乱你的助手 - 另外,你可以在其他地方重用它以在其他模板中实现简单的逻辑。
我同意 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 %>