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

发布于 2024-09-10 15:43:34 字数 801 浏览 5 评论 0原文

从 PHP 迁移到 Rails 后,我对 Rails 很陌生,并且遇到了无尽的挫折,但希望有一个陡峭的学习曲线。

我正在遵循有关如何在 Rails 中制作 Twitter 克隆的指南,并继续沿着这条道路使其变得越来越像 Twitter。

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

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

当然,我得到了一个非常简单的东西

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

,但它不起作用,但在它的正上方,我输出了 session[:user_id] 和 params[:id],并且打印输出完全相同。

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

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

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

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

输出如下所示,

-4c4483ae15a7900fcc000003-
-4c4483ae15a7900fcc000003- 

这是用户的 mongodb objectId,两侧带有破折号,表明没有空格或任何内容。

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.

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.

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北恋 2024-09-17 15:43:34

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

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

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

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

?

居里长安 2024-09-17 15:43:34

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

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

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

另外,您可能希望将该比较移至控制器中。比如:

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

然后你可以放入你的视图:

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

它使代码更容易阅读。

It could be like jasonpgignac pointed out. If you enter into 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.

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.

兲鉂ぱ嘚淚 2024-09-17 15:43:34

正如已经指出的,大多数情况下此类问题是由比较类型不匹配引起的。只需将 to_s 添加到两个变量即可解决大多数情况。

此处查看重要资源,了解有关 Ruby 中使用的各种比较的更多信息。

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文