Rails - 如何在控制器方法之间保留实例变量

发布于 2024-10-29 07:58:07 字数 1227 浏览 5 评论 0原文

我正在学习 Rails,并创建一个简单的问题/答案应用程序来测试我所介绍的一些概念。我使用生成脚手架创建两个模型类 - 问题和用户。

问题表由question_number(int)、question_text(string)和answer_text(string)组成。

users 表有一个 uid(字符串)、name(字符串)和 current_question(int),

然后我创建了一个名为 gateway 的控制器。我的应用程序现在正在运行,用户可以进入主页,将其 UID 作为参数传递(最终将通过登录函数传递),并且应用程序将通过抓取与用户的当前问题。伟大的。

然后我在网关控制器中创建了一个应答方法。此方法将用户在表单中输入的答案与数据库中的答案进行比较,根据答案是对还是错将用户重定向到不同的页面。

我的问题 - 我在索引方法中设置实例变量来设置当前用户和当前问题 - 但这些不会持续到我的答案方法中。 Rails 的最佳实践是什么?我是否:

1)通过表单将这些作为参数传递给答案方法,就像我传递答案一样(当我已经在控制器中创建了实例变量,现在将它们传回时,这似乎效率低下......) 2)将它们存储为会话中的变量? 3)将这些存储在Flash中? 4)只需在答案方法中再次查找它们(不喜欢重复代码,并且两次调用相同信息似乎很浪费)

我什至为这个应用程序使用了正确的结构吗?在这种情况下,您想要获取用户的属性(他们当前的授权问题)并多次使用它来查询数据库的另一部分(步骤 1 - 获取与用户的授权问题匹配的问题,步骤 2 -检查用户输入的答案是否正确,步骤 3 - 增加用户的身份验证问题计数器)

更多信息,@wukerplank

没有用于会话处理的插件。基本上,我的流程是这样的:

  1. “登录”用户(这个小测试应用程序中没有登录逻辑,所以我只是将 uid 作为 URL 中的参数传递)
  2. 网关控制器使用 @question = Question.where 获取用户当前的问题(:question_number => @user.current_question).first
  3. 用户显示问题文本和输入答案的表单
  4. 提交按钮调用我的 :answer 操作并传递答案字符串
  5. 这是我想再次调用 @question 的地方, 但不能,因为它没有持久化。所以我要么必须重复步骤2,要么找到一个地方来存储我需要再次引用的数据。我假设 Rails 的 DRY 原则建议不要重复步骤 2,所以我想知道存储这些(以及我可能需要的其他变量)的最佳方法在哪里/如何 谢谢!

I'm learning rails, and creating a simple question/answer app to test some of the concepts I've been introduced to. I used generate scaffold to create two model classes - Question and User.

The questions table consists of a question_number (int), question_text (string) and answer_text (string).

The users table has a uid (string), name (string) and current_question (int)

I then created a controller called gateway. I have the app working now where a user can go to the main page passing their UID as a parameter (eventually will be passed by a login function) and the app will display the question they are currently working on by grabbing the question_number which matches the user's current_question. Great.

I then created an answer method in the gateway controller. This method takes an answer the user types into a form and compares it to the answer in the database, redirecting the user to different pages depending on if the answer was right or wrong.

My issue - I set instance variables in the index method to set the current user and current question - but these do not persist to my answer method. What is the Rails best practice here? Do I:

1) Pass these as parameters to the answer method through the form, just like I pass the answer (seems inefficient when I've already created the instance vars in the controller, and now pass them back...)
2) Store these as vars in the session?
3) Store these in Flash?
4) Just look them up again in the answer method (don't like repeating code, and 2 calls for same info seems wasteful)

Am I even using the right structure for this app? What would you do in this instance, where you want to grab an attribute of a user (their current authorized question) and use it several times to query another part of the database (step 1 - get question matching user's authorized question, step 2 - check that answer user inputs is correct, step 3 - increment the user's auth question counter)

more info, @wukerplank

No plugins for session handling. Basically, my flow goes like this:

  1. "login" user (no login logic in this little test app, so I just pass the uid as a param in the URL)
  2. The gateway controller gets the user's current question with @question = Question.where(:question_number => @user.current_question).first
  3. User is displayed the question text and a form to enter answer
  4. Submit button calls my :answer action and passes the answer string
  5. Here is where I'd love to just call @question again, but can't because it is not persisted. So I either have to repeat step 2, or find a place to store the data I need to reference again. I'm assuming the DRY principles of Rails would advise against repeating Step 2, so I'm wondering where/how the best way to store these (and other variables I may need) are
    Thanks!

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

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

发布评论

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

评论(2

原野 2024-11-05 07:58:07

听起来将其存储在会话中是合适的,具体取决于您想要多少安全性。

在rails3中,会话实际上存储在cookie本身中并且大小有限。
如果变量是大对象,则在数据库中创建一个表,并将 id 存储在会话中并每次查找它。

或者,您可以使用 memcached 或等效工具来存储会话,然后 cookie 只存储会话 ID。

Sounds like storing it in a session would be appropriate, depending on how much security you want.

In rails3 the session is actually stored in the cookie itself and has limited size.
If the variables are large objects then create a table in the database and just store the id in the session and look it up each time.

Alternatively you could use memcached or equivalent to store the session and then the cookie just stores a session ID.

挽袖吟 2024-11-05 07:58:07

我可能误解了你的问题,但通常的程序是:

  • 登录用户,
  • 将其 ID 存储在会话中
  • 通过数据库中的 ID 在会话中重新加载每个请求,将
  • 相关信息(测验进度等)存储在用户表

您是否使用插件进行会话处理(Devise、Authlogic 等)?

I probably misunderstand your question, but the usual procedure would be:

  • Log in the user
  • store his ID in the session
  • reload the user with every request from the DB via his ID in the session
  • store relevant information (progress in quiz etc.) in the user table

Do you use a plugin for the session handling (Devise, Authlogic, etc.)?

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