以下所有尝试处于Ruby 1.9 Rails 3.2
1) Rails机制
Server最先使用config/environment.rb
2)Rails路由
除了基础那些show edit new 的功能之外的action和页面,都使用config/routes.rb中的定义映射。
当请求发到Server,Rails会先去public文件夹中加载直接的页面和文件。当找不到时,就会找routes.rb了。根据路由配置,将请求转化为对Controller中的Action并调用之。
看到过集中配置:
get "demo/home" 意思是将对demo/home的请求发至demo controller下的home action。但我尝试这个不成功。
match "/home", :to => "demo#home"
这个成功。意识是当url为/home时,将调用demo controller下的home action。
而此时,这个url也就变成了home_path 在系统中使用
3)link_to
实际应用中常常看到这样的代码:
.html.erb上
<%= link_to('AA', {:controller => "AA", :action => 'index', :type => 'typeA'}) %>
这将有个链接<a> 内容是AA 链接指向 /AA/index?type=typeA
4) link_to流程
<%= link_to "RR", home_path %>
链接指向/home页。如果public里没有,则去routes.rb找。如2)中找到match "/home"项,则去demo controller home action。进行操作后若无render等指向型语言,则直接到home.html.erb的view进行展示。
action中若有render,例如
render :action => 'EE'
则会到本controller的view文件夹中EE.html.erb进行展现。
render的是不会执行controller方法的,直接进行view页面渲染,且URL仍是原来的,不会改变成EE。若使用redirect_to则会改变url
5)MVC名称问题
Controller file:static_pages_controller.rb (page为复数)
Controller class: StaticPagesContrller
Model file: static_page.rb (page为单数)
Model class: StaticPage
URL: /static_pages/home
View file: static_pages目录下home.html.erb or home.rhtml 目前没发现这两个后缀有啥区别