Rails MVC 概念的问题
呃 - 不知道为什么我在这方面遇到这么多麻烦。
编写一个简单的问答应用程序(请参阅 Rails - 这段代码应该放在哪里才能保持 MVC 设计的真实性? 了解一些细节)
尝试坚持 MVC 原则和正确的设计 - 这个应用程序只是一个学习体验,所以我想确保我以普遍接受的方式做事
引用的问题给了我建议,将我的功能分成不同的模型。然而,在尝试实现这一点时,我发现自己到处传递参数,而且我只是感觉我没有做正确的事情。所以,这是应用程序的基本布局和我想要完成的任务 - 如果有人能让我知道我是否走在正确的轨道上......
问题模型:包含 id(pkey)、question_number(int)、question_text (string)、answer_text(string)
用户模型:包含:id(pkey)、uid(string)、current_question(int)、name(string)
我用脚手架创建了上述两个内容,因此它们具有所有默认路由、控制器操作等等......
我创建了一个网关控制器并通过routes.rb将其设置为我的默认页面。
这个想法是,用户浏览到localhost:3000 /?uid =“whatever”,索引页面显示当前问题(Question.rb)。 find_by_question_number(@user.current_question))
用户在表单中输入答案,该答案将其发布到操作。在我的初稿中,这在网关控制器中调用了一个操作,该操作检查答案是否正确。
现在,我试图在上一个问题中采纳瓦迪姆的建议,并将用户登录保留在用户中,并保留有问题的问题逻辑。现在我的表单 POST 到用户控制器。
这就是我感到困惑的地方。逻辑代码不应该在控制器中,对吧?因此,我调用模型中的一个方法,传递用户答案和问题 ID 之类的内容,因为我无法读取模型中的会话。这工作得很好,我现在可以处理用户模型中的逻辑 - 所以现在用户模型调用问题模型中的方法来实际检查答案。这意味着我必须使用我传递的 ID 实例化我的问题对象,然后调用另一个方法,(再次!)传递答案。等等...等等...
明白我的意思吗?从理论上讲,我确实理解 MVC 的价值,但每当我尝试实现它时,我都会遇到这样的混乱。这是正确的,因为我的程序太简单了,这似乎让事情变得过于复杂?
有人可以告诉我如何分割逻辑吗?您不需要发布实际的代码,只需发布您将放在哪里的代码,例如:
网关控制器: -向用户显示问题 -接受答案并传递给 XXX 控制器
XXX 控制器: -在XXX模型中调用方法Foo,传递X和Y
基本流程应该是,向用户显示一个问题,用户回答问题,答案与问题模型中的正确答案进行比较,根据结果返回消息,然后回答是正确的,用户的 current_question 增加。
非常感谢您的帮助,我有书籍和谷歌,并且一直在阅读我的a$$折扣,只是迷失在这里。这是我第一次尝试在预先编写的示例代码的安全之外进行冒险,所以请保持温柔!
谢谢!!
Ugh - not sure why I'm having so much trouble with this.
Writing a simple question and answer app (see Rails - Where should this code go to stay true to MVC design? for some details)
Trying to stick to MVC principles and proper design - this app is simply a learning experience, so I want to make sure I'm doing things in a generally accepted way
The referenced question gave me advice to split up my functionality into the different models. However, trying to implement this, I find myself passing parameters all over the place and I just get a feeling that I'm not doing something right. So, here's the basic layout of the app and the tasks I am trying to accomplish - if someone could let me know if I'm on the right track...
Question Model: contains id(pkey), question_number(int), question_text(string), answer_text(string)
User Model: contains: id(pkey), uid(string), current_question(int), name(string)
I created both of the above with scaffold so they have all the default routes, controller actions, etc...
I created a gateway controller and set it to be my default page through routes.rb
The idea is, user browses to localhost:3000/?uid="whatever" and the index page displays the current question (Question.find_by_question_number(@user.current_question))
User enters answer in a form, which POSTs it to an action. In my first draft, this called an action in the gateway controller which checked if the answer was correct.
Now, I am trying to take vadim's advice in my last question and keep the user login in user and the question logic in question. So now my form POSTs to the users controller.
Here's where I get mixed up. The logic code shouldn't be in the controller, right? So I call a method in the model, passing it stuff like the user's answer and the question id since I can't read the session in the model. This works fine, I can now take care of the logic in the user model - so now the user model calls a method in the question model to actually check the answer. That means I have to instantiate my question object using the ID I passed, then call another method, passing (again!) the answer. Etc...etc...
See what I mean? I definitely understand the value of MVC in theory, but whenever I try and implement it I wind up with a mess like this. It this correct, and it just seems like overcomplicating things because my program is so simple?
Can someone walk me through how you would split the logic up? You don't need to post actual code, just what you would put where, like:
Gateway Controller:
-display question to user
-take answer and pass to XXX controller
XXX controller:
-call method Foo in XXX model, passing X and Y
The basic flow should be, user is shown a question, user answers question, answer is compared to the correct answer in the question model, message is returned based on result, and it answer was correct, user's current_question is incremented.
Thanks so much for the help, I have books and Google and have been reading my a$$ off, just lost in the sauce here. This is my first attempt to venture outside the safety of pre-written example code, so please be gentle!!
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在大多数情况下,在问答应用程序中,您将有一个问题模型、一个答案模型和一个用户模型。您的操作是:
一些代码:
路由
answers_controller
In most cases in a Q&A app, you would have a Question Model, an Answer Model, and a User model. Your actions are:
Some code:
Routes
answers_controller
基本流程应该是:
现在到模型。您可以在其中放置检查某些内容的方法。尽管如此,控制器仍然处理工作,它调用方法并在控制器中处理结果。
The basic flow should be:
Now to the model. You can put methods in there that check certain things. Still, the controller handles the work, it calls the method and processes the results in the controller.