简体   繁体   English

当前用户个人资料视图和其他用户的Rails if语句

[英]Rails if statement for current user profile view and other users

Hi I'm having some trouble getting different views to display based on whether or not the user being viewed is the logged in user. 嗨,我在根据要查看的用户是否是登录用户获取不同的视图来显示时遇到了一些麻烦。

I'm using Rails 3.2 and Devise 我正在使用Rails 3.2和Devise

I've tried the code below but it always returns the template for the logged in user (where I have edit info links) rather than the one for the other users. 我尝试了下面的代码,但它始终返回已登录用户的模板(我在其中具有编辑信息链接),而不是其他用户的模板。

<% if user = current_user %>  
  <%= render 'users/myprofile' %>
<% else %>
  <%= render 'users/viewprofile' %>
<% end %>

Thanks very much 非常感谢

The statement should be.. 该声明应是..

<% if user == current_user %>  

The reason being, = is for assignment, == is for comparison. 原因是= =用于分配,==用于比较。

Check the sign of comparison. 检查比较符号。 You have used equals to sign. 您已使用等于进行签名。

<% if user == current_user %>

= sign assigns the current user to user variable. =符号将当前用户分配给user变量。 Hence the condition will always be true unless there is no current user. 因此,除非没有当前用户,否则条件将始终为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM