Rails-该代码在MVC设计中应遵循哪些原则? [英] Rails - Where should this code go to stay true to MVC design?

查看:44
本文介绍了Rails-该代码在MVC设计中应遵循哪些原则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,并且通过几本书来工作,并在编写小型测试项目时编写代码.我最新的开发应用程序是一个问答程序.这个想法是让数据库表充满问题.用户将打开该Web应用程序,并向其提出第一个问题.用户在表格中输入答案,如果答案正确,他将获得下一个问题.很简单.

I'm new to Rails, and working my way through a couple of books, coding small test projects as I go. My latest dev app is a question and answer program. The idea is to have a database table full of questions. A user will bring up the web app and be presented with the first question. User enters answer into a form, and if it is correct, he gets the next question. Pretty simple.

因此,我使用了生成支架来创建问题表和用户表.效果很好-我可以导航到系统生成的视图以添加/编辑/删除问题和用户.

So, I've used generate scaffold to create a questions table and a users table. This works well - I can navigate to the system-generated views to add/edit/delete questions and users.

然后,我创建了一个名为网关的新控制器.我将其作为我的根控制器,并为其指定了索引方法.索引方法将用户ID作为URL参数,并为用户获取适当的问题.然后,它会加载一个显示问题的视图,并具有用户输入答案的表格.

I then created a new controller called gateway. I made this my root controller and gave it an index method. The index method takes in a user ID as a URL parameter and fetches the appropriate question for the user. It then loads a view which displays the question and has a form where the user enters the answer.

我现在正在编程逻辑以测试答案-我正在网关控制器中使用一种称为"test_answer"的新方法来执行此操作,但是我觉得我现在正在破坏MVC设计,因为此代码不是直接的与显示视图有关.我对应该在哪里放置这样的代码感到困惑(该方法测试答案,如果正确将增加用户级别,如果错误将重定向到错误页面)

I'm now programming the logic to test the answer - I was doing this in a new method called "test_answer" in the gateway controller, but I feel like I'm now breaking MVC design, since this code is not directly related to displaying a view. I'm a bit confused as to where I should put code like this (the method tests the answer, if correct increments the user's level, if wrong redirects to an error page)

应该在现有模型(用户或问题)中使用吗?它应该留在网关控制器中吗?我应该在/lib中创建一个新文件吗?

Should it go in an existing model (user or question)? Should it stay in the gateway controller? Should I create a new file in /lib?

我了解了MVC的一般概念(视图显示信息,模型存储和操纵数据,控制器处理两者之间的交互),但是我不确定这段代码是否适合.

I get the general concept of MVC (views display info, models store and manipulate data, controllers handle interactions between the two) but I'm not sure how this code fits in.

谢谢!

作为参考,这是我当前的gateway.rb控制器-请分拆!我确定里面有很多可疑的地方...

For reference, here is my current gateway.rb controller - please, pick it apart! I'm sure there are a number of questionable bits in there...

class GatewayController < ApplicationController
  def index
    if params[:uid]
      @user = User.where(:uid => params[:uid]).first
    else
      @user = User.where(:uid => "000").first
    end  
    @question = Question.where(:question_number => @user.current_level).first
    session[:uid] = @user.uid
    session[:answer] = @question.answer_text
  end

# POST /gateway/answer
# POST /gateway/answer.xml
  def answer
    #below is code I feel shouldn't be here...the logic to test the answer and then increment user's level
    if (params[:user_answer]) == session[:answer]
      @user = User.where(:uid => session[:uid]).first
      @user.increment!(:current_level)
      respond_to do |format|
        #code here will redirect to "Correct answer" page
      end
    else
      respond_to do |format|    
        #code here will redirect to "Wrong answer" page
      end
    end
  end
end

推荐答案

我会将那个test_answer放入模型中,因为它与数据有关.这将返回一个布尔值,然后控制器将在该布尔值上进行比较,以确定是否重定向到错误页面.

I would put that test_answer in your model because it is concerning the data. This will return a boolean value in which the controller will then compare on, to determine whether or not to redirect to an error page.

半伪代码:

class Question
  def test_answer(arg)
    if answer_is_right
      user.incr_level
    else
      false
    end
  end
end

class GatewayController < ApplicationController
  #code

  def answer
    if !question.test_answer(param)
      redirect_to error_page_path
    end

  end
end

这篇关于Rails-该代码在MVC设计中应遵循哪些原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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