在 Rails 中访问另一个控制器中的一个控制器变量 [英] Accessing one controller variable in another controller in Rails

查看:42
本文介绍了在 Rails 中访问另一个控制器中的一个控制器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 C 编程背景,我正在为一个业余项目学习 Ruby/Rails.目前我正在努力理解变量的范围是如何工作的.这是我遇到的一个问题的示例.我有一个 User 和一个 Product 模型.在产品数据库中,我存储了不同的产品,其中一个字段是与存储在用户表中的用户 ID 匹配的用户 ID 字段.用户登录后 - 我显示用户的姓名、电子邮件等(这存储在@current_user 中),然后有一个链接来显示用户拥有的不同产品.我通过 users/show.html.erb 中的以下代码执行此操作,如下所示:...

姓名:<%= @current_user.name %>

I have a C programming background and I am learning Ruby/Rails for a side project. Currently I am struggling to understand how the scoping of variables works. Here is an example of a problem I am hitting. I have a User and a Product model. In the Product DB I store different products and one of the fields is a userid field that matches the userid stored in the User table. After the user logs in - I display the user's name, email etc (this is stored in @current_user) and then have a link to show different products owned by the user. I do this via the following code in users/show.html.erb like this: ...

Name: <%= @current_user.name %>

到目前为止一切顺利.当我单击产品"链接时,它会将我带到 products/index.html.erb 并执行以下代码(我想过滤此显示以仅显示 @current_user.userid 拥有的产品.我该怎么做?以下代码给了我一个运行时错误,因为 @current_user 评估为 nil 我相信.如果我删除if"子句,那么所有产品都会显示出来,这不是我想要的.感谢您提供任何提示.

So far so good. When I click the "Products" link it takes me to products/index.html.erb and the following code gets executed (I want to filter this display to show only products owned by @current_user.userid. How do I do this? The following code gives me a runtime error as @current_user evaluates to nil I believe. If I remove the "if" clause then all products ar displayed which is not what I want. Would be grateful for any tips.

<tbody>
<% @products.each do |product| %>
  <tr>
    <td><%= product.userid if product.userid == @current_user.userid %></td>
    <td><%= product.productName if product.userid == @current_user.userid %></td>
    <td><%= product.productLink if product.userid == @current_user.userid %></td>

谢谢,

推荐答案

我本想把它作为评论,但我认为它值得回答.不仅要了解 Rails,还要了解整个互联网,HTTP 是无状态的.这意味着在您的控制器中,您获取用户的信息,为他们构建一个包含数据库中所有内容的网页,然后将其发送给他们.然后他们在页面上做一些事情并向另一个页面或更多数据发送请求,你的 rails 应用程序就像哦,嘿!你是谁?"因为你用好的信息填充的所有变量都消失了.每次你的应用收到一个请求时,它都会创建一个它正在使用的控制器的全新实例,然后删除它,连同里面的所有东西.这很重要,因为目前可能有数千或数百万人登录到您的终极飞盘联盟网站,试图找出周六的比赛地点.而且您想将正确的信息提供给正确的人,您不能假设您上次向其发送信息的人就是现在要求您提供新信息的人.

I was going to make this a comment, but I think it is worthy of an answer. The thing to understand about not just rails, but the entire internets, is that HTTP is stateless. This means that in your controller you grab the user's information, build them a webpage with all of their stuff from the database, and send it out to them. Then they do something on the page and send a request in for another page or more data, and your rails app is like "Oh hai! Who are you?" Because all those variables that you filled up with nice information are just gone. Every time your app receives a request, it makes a brand new instance of the controller it is using, and then gets rid of it afterwards, along with everything inside it. And this is important, because there might be thousands, or millions of people currently logged into your ultimate frisbee league website trying to figure out who is playing where on Saturday. And you want to give the right info to the right person, you can't assume that the person you last sent information to is the same person asking you for new info now.

所以我们需要使用session,每次你发送一个页面时都会传递给客户端浏览器的一些信息,浏览器会在每次请求时将它发送回给你.您在会话中放置了足够的信息,以便在收到响应时可以使用它从服务器重建您需要的信息.我会让这变得非常简单,然后将您推荐给 Rails 安全指南获取一些更重要的信息.实际上,我只会使用他们的示例:

So we need to use the session, some information that gets passed to the client browser every time you send out a page, and the browser sends it back to you with every request. You put enough info in the session so that you can use it to rebuild what you need from the server when you get a response. I'm going to make this real simple and then refer you to the Rails Security Guide where you can get some more important information. Actually, I'll just use their example:

您可以通过这种方式将用户 ID 添加到会话中:

You can add the user id to the session this way:

session[:user_id] = @current_user.id

然后以这种方式取回:

@current_user = User.find(session[:user_id])

然后您使用您的用户模型(在本例中)从数据库中获取您需要的特定信息.这是 Rails 指南的另一个链接,因为它非常好,我链接了两次:http://guides.rubyonrails.org/security.html#what-are-sessions-questionmark

And then you use your user model (in this case) to get the specific info you need from the database. Here's another link to the Rails guides because it's so nice, I'm linking it twice: http://guides.rubyonrails.org/security.html#what-are-sessions-questionmark

如果你正在做一些需要密码的事情,你可以使用像 devise此处的说明,但请注意,它完全劫持了您的用户模型、会话控制器、一些视图,基本上所有与登录和身份相关的内容.定制并不容易,但它使所有这些东西变得更加复杂,实现起来非常简单.如果您不需要任何用户名或密码并且您正在做一些非常简单的事情,只需跳过特殊的宝石并自己使用会话.

If you are doing something that requires a password you can use a gem like devise, instructions here, but be warned, it totally hijacks your user model, your sessions controller, some views, basically all things related to logging in and identity. It isn't easy to customize, but it makes all this stuff and things much more complicated very simple to implement. If you don't need any usernames or passwords and you are doing something real simple just skip the special gems and use the session yourself.

这篇关于在 Rails 中访问另一个控制器中的一个控制器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆