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

时间:2022-12-05 12:55:34

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迁移到摇滚乐之后我就开始崭露头角了,但是对于挫折感已经无止境了,但希望有一个陡峭的学习曲线。

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.

当然,这不起作用,但正好在它之上我输出了会话[:user_id]和参数[: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

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

-<% 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,左侧有破折号,表示没有空格或任何东西。

3 个解决方案

#1


16  

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

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

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

?

#2


2  

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.

确保它们都是相同的类型。设置<%= 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.

它使代码更容易阅读。

#3


0  

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

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

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

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

#1


16  

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

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

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

?

#2


2  

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.

确保它们都是相同的类型。设置<%= 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.

它使代码更容易阅读。

#3


0  

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

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

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

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