Ruby-on-rails rails 比较 params[:id] 和 session[:user_id] 的值不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3283292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:56:08  来源:igfitidea点击:

rails comparing values of params[:id] and session[:user_id] not working

ruby-on-railsstring-comparison

提问by pedalpete

I'm new to rails after moving from PHP and am having no end to the frustrations, but hopefully there is a steep learning curve.

在从 PHP 迁移之后,我是 Rails 的新手,并且没有尽头的挫败感,但希望有一个陡峭的学习曲线。

I was following a guide on how to make a twitter clone in rails, and have continued along that path making it more and more twitter like.

我一直在关注如何在 rails 中制作 twitter 克隆的指南,并继续沿着这条道路前进,使其越来越像 twitter。

So I've got a 'users' page /users/show.html.erb which show all the posts from a user.

所以我有一个“用户”页面 /users/show.html.erb 显示来自用户的所有帖子。

Now, if the currently logged in user is the same as the page owner, I'm trying to show the text box so that the user can add a new entry.

现在,如果当前登录的用户与页面所有者相同,我会尝试显示文本框,以便用户可以添加新条目。

I've got what should be a very simple

我有什么应该是一个非常简单的

<% if params[:id] == session[:user_id] %>
    put the text box here
<% end %>

of course, that isn't working, but right above it I've output both the session[:user_id] and the params[:id], and the printout is exactly the same.

当然,这是行不通的,但在它的正上方我输出了 session[:user_id] 和 params[:id],并且打印输出完全相同。

If I set the == to !=, I get the 'put the text box here' message.

如果我将 == 设置为 !=,我会收到“将文本框放在这里”的消息。

any suggestions as to what I'm doing wrong? I know these two values match, as I can see in the url and the output of the currently logged-in user. I've also output

关于我做错了什么的任何建议?我知道这两个值是匹配的,正如我在 url 和当前登录用户的输出中看到的那样。我也输出

-<% session[:user_id] %>-
-<% params[:id] %>-

so that I can see there is no gaps or spaces or other characters on either end of the parameters, and it all looks clean.

这样我就可以看到参数两端没有间隙、空格或其他字符,而且看起来都很干净。

The output looks like this

输出看起来像这样

-4c4483ae15a7900fcc000003-
-4c4483ae15a7900fcc000003- 

which is the mongodb objectId of the user with dashes on either side to show that there are no spaces or anything.

这是用户的 mongodb objectId,两边都有破折号,表示没有空格或任何东西。

回答by jasonpgignac

Are you sure that both items are simple strings? What happens if you run, say

你确定这两个项目都是简单的字符串吗?如果你跑会发生什么,说

params[:id].to_s == session[:user_id].to_s

?

?

回答by Charles Caldwell

It could be like jasonpgignac pointed out. If you enter into IRB:

这可能就像 jasonpgignac 指出的那样。如果您进入 IRB:

num = "7"
num2 = 7
num == num2 # => false

Make sure they are both of the same type. Putting <%= num2 %>will actually trigger the .to_s method... hence why the two appear to be equal when you output them in your .erb page.

确保它们的类型相同。Putting<%= num2 %>实际上会触发 .to_s 方法......因此,当您在 .erb 页面中输出它们时,为什么两者看起来相等。

Also, you might want to move that comparison into the controller. Something like:

此外,您可能希望将该比较移动到控制器中。就像是:

@is_user_home = params[:id].to_s == session[:user_id].to_s

Then you can put in your view:

然后你可以把你的观点:

<% if @is_user_home %>
    code here
<% end %>

It makes the code a little more easier to read.

它使代码更易于阅读。

回答by w1t3k

As it was already stated, in most cases this kind of problems are caused by mismatch of compared types. Just adding to_sto both variables solves most cases.

如前所述,在大多数情况下,此类问题是由比较类型的不匹配引起的。只需添加to_s到两个变量即可解决大多数情况。

Herecheck great source to learn more about all kind of comparisons used in Ruby.

在这里查看很好的源代码以了解有关 Ruby 中使用的所有类型的比较的更多信息。