Self Summary: Ruby on Rails and MVC architecture

本文深入探讨了Ruby on Rails中MVC架构的实现,包括控制器、模型和视图的作用,以及如何通过资源命令生成资源、控制器、模型和视图。详细解释了控制器如何响应请求,模型如何实现业务逻辑,以及视图如何呈现数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How does Ruby on Rails represent MVC architecture?


Well, MVC means Model, Views and Controller. If we use the rails generate (projectName) to generate a rails project, we can see the following architecture[1]. 




We can see there are model, views and controllers in the app folder. 


Controller: 


In rails, controller is to choose how to response a particular request(By defining actions). Writing in the ruby, controller must be sub-class of ApplicationController. And use the 'def' to define different functions of this class/object. We can those functions as actions


Different actions usually mean different requests about one resources. In rails, we have a definition of resources. A resource normally means that it can be CRUD(Create, Read, Update and Delete). This is called REST architecture.  We can tell that CRUD are different actions. Therefore, we need to define different response respect to different request.


Normally, you can use the command: rails g resource xxx


However, after you have run the command above, it can generate a resources in the routes.rb. It will have the setting like below[1]: 




Also, it will create the model, views and also the controller. However, the content of model and controller are empty. And it only create the folder of this resources in the views. 


Normally, the code inside an action will be like:


1) create the object of its corresponding model class. In the model class files, there are functions which can execute some logics we want to achieve. 

2) use the object to call up some logic functions.

3) generate the appropriate views by using codes like this: 

respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comments }

If you call the view like above, it will automatically call the views which named by the name of 'def' you are writing inside now. So if you are writing in the controller named comments and an action called index, then it will call the view which is located under folder comments and named as index.html.erb. By the way, erb means embedded ruby. 


There is a regulation of finding the view of a particular action. Here is some content I stole from [1].




Models: 


Models in rails inherit from the class of ActiveRecord:Base. As I have said above, the main purpose of model is to declare some useful function. There are a lot of special grammar in model. Like: attr_accessible, validates, attr_ccessor, cattr_accessor, scope, hasmany and etc. 


Here is a good summary of some of it: http://blog.youkuaiyun.com/menxu_work/article/details/14166923


More details about these sentences can be found in: http://api.rubyonrails.org/


Here is also some basic background knowledge about ruby[2]:


1、实例变量
      Ruby规定实例变量是以@开头,后跟一个名字。对实例变量的访问注意
        a、在类内部可以对其赋值和读取没有限制
        b、类外不能直接访问对象的实例变量,只能通过方法访问。正如上面所说的属性访问方式一样。
        c、实例变量在使用前不用必须初始化。
2、类变量
       
       Ruby规定类变量是以两个@@开头,后跟一个名字。对于一个给定的类而言,类变量只存在一份拷贝。
       注意:
         1、类变量必须在使用之前要初始化。
         2、类变量是私有的。要想能从类外部访问,必须定义方法访问。这个方法是一个实例方法,要么是一个类方法。
         3、通常类变量属于包含该变量的最内层的类或模块。但是,可以为被定义该变量的类的所有子类所共享

3、类常量
     Ruby 的常量也是对对象的引用,常量通常在第一次被赋值的时候创建(通常是在类或模块中定义)。和其他语言不一样,
     Ruby允许改变常量的值,因为常量是对对象的引用,通过改变常量的值,可以改变它所引用的对象的内部状态。
     1、在类或模块中的任何位置都可以直接访问该类或模块中定义的常量。
     2、在类或模块外,可以通过在域作用符::前加上一个返回适当类或模块对象的表达式来访问。
     3、对于不属于任何类或模块的常量,可以直接访问或使用不带前缀的域作用符来访问
     4、通过在常量名之前使用类或模块名和域作用符,还可以将常量从外面添加到已存在的类或模块中。
     注意:
     1、类名本身就是常量。这就意味着可以把类和其他Ruby对象一样对待:可以拷贝它们;将它们作为参数传入方法;或者在表达式中使用它们
     2、如果一个无名的类赋值给了一个常量。Ruby将常量作为类名。


attr_accessor: 该方法自动创建两个方法,一个用于读取属性值,一个用于修改属性值
4、方法 
     1、实例方法
       实例方法的定义就是通常的方法定义。其作用域就是每个特定的对象。用于访问该对象的内部状态和行为。
       实例方法的定义是保存在对象所属类对象的定义中。如果没有,就到类对象的父类对象中查找。依次类推
     2、类方法
       在方法名前放置类名以及一个点,来定义类方法
        def classname.methodname
        .....
        end
        类方法的定义是保存在类对象的虚拟类中。当调用类方法时,是到虚拟的类中寻找。如果没有,就到虚拟类的父类中查找。依次类推。    


Also, here is something from[3]: 


注意: 


值得注意的是,ruby中没有"++"、"--"一类的运算符,但可以通过"+=1"、"-=1"实现。


ruby标识符的一些约定:


局部变量以小写字母或者下划线开头


全局变量以美元符号开头。


实例变量以@开头


类变量以@@开头


常量或类名以大写字母开头。


ruby中的nil关键字很特别。nil表示一个与其他语言中null相类似的空集的概念。和其他语言不通的是,ruby在逻辑判断过程中,只有nul和false表示假,其他所有表达式都表示真。 


Views: 


Views are also called templates. Most of the templates are formatted in html. But they need handler of erb, builder or coffee. Here I want to talk about the form builder. 


form_for is a useful form builder in rails. 


Here is a sample code of form of view I copied from[1]: 


<%= form_for :article do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>
 
  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>
 
  <p>
    <%= f.submit %>
  </p>
<% end %>

 

Here is the explanation[1]: 


调用 form_for 方法时,要指定一个对象。在上面的表单中,指定的是 :article。这个对象告诉 form_for,这个表单是用来处理哪个资源的。在 form_for 方法的块中,FormBuilder 对象(用 f 表示)创建了两个标签和两个文本字段,一个用于文章标题,一个用于文章内容。最后,在 f 对象上调用 submit 方法,创建一个提交按钮。


However, if you only uses the code above, after you push the submit button, it will go to the page itself. Because the action value is still /articles/new. 


这个问题可使用 form_for 方法的 :url 选项解决。在 Rails 中,用来处理新建资源表单提交数据的动作是 create,所以表单应该转向这个动作。


Change the code head to <%= form_for :article, url: articles_path do |f| %>. 


articles_path 帮助方法告诉 Rails,对应的地址是 /articles,默认情况下,这个表单会向这个路由发起 POST 请求。这个路由对应于 ArticlesController 控制器的 create 动作。


About the other options of form_for builder. You can turn to this page: http://www.codesky.net/article/200910/166574.html


Reference: 


[1] http://guides.ruby-china.org/getting_started.html#the-mvc-architecture

[2] http://blog.163.com/chenxiangtao@126/blog/static/7770746820091114550803/

[3] http://blog.youkuaiyun.com/emerald0106/article/details/7358967



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值