[英]Ruby on Rails If…Else
I have the following code: 我有以下代码:
<div class="category_name">
<%= current_user %>
<%= review.user.front_name %>
<% if current_user.name = review.user.front_name %>
<%= link_to "You", user_path(review.user) %> / <%= review.categories_list %>
<% else %>
<%= link_to review.user.front_name, user_path(review.user) %> / <%= review.categories_list %>
<% end %>
</div>
It renders something like: 它呈现出类似以下内容:
Tom Murphy Bill Smith You / Films 汤姆·墨菲·比尔·史密斯You /电影
Why is this happening? 为什么会这样呢?
<% if current_user.name = review.user.front_name %>
In other words, if Tom Murphy = Bill Smith...which it doesn't...shouldn't it go to the line AFTER the 'else'? 换句话说,如果汤姆·墨菲(Tom Murphy)=比尔·史密斯(Bill Smith)...那不是...难道不是在“其他”之后到那一行吗? That's what I want to happen.
那就是我想发生的事情。
replace <% if current_user.name = review.user.front_name %>
with 将
<% if current_user.name = review.user.front_name %>
替换为
<% if current_user.name == review.user.front_name %>
you put =
, you have to put ==
你放
=
,你必须放==
In Ruby to check equality you should use double equal signs ( ==
). 在Ruby中,检查相等性应使用双等号(
==
)。
Also - Currently you are comparing the names. 另外-当前,您正在比较名称。 You should compare the objects (User) to avoid mistakes or even worse (security breaches).
您应该比较对象(用户),以避免错误甚至更糟(违反安全性)。
<% if current_user == review.user %>
Just modify: <% if current_user.name = review.user.front_name %>
to 只需修改:
<% if current_user.name = review.user.front_name %>
更改为
<% if current_user.name == review.user.front_name %>
The ==
( double equal) Test for equal value.. ==
(double equal)测试是否相等。
when you want to make conditional statement. 当您要进行条件声明时。 you should compare your statement.
您应该比较您的陈述。 for example:
例如:
if number == 10
print "Variable is 10"
elsif number == "20"
print "Variable is 20"
else
print "Variable is something else"
end
on your case please change the line: 根据您的情况,请更改行:
<% if current_user.name = review.user.front_name %>
change intoenter code here 在此处更改代码
<% if current_user.name == review.user.front_name %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.