仍然很难使用 RoR MVC 方法 [英] Still having a hard time with RoR MVC approach

查看:32
本文介绍了仍然很难使用 RoR MVC 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为应该公正地陈述我认为我到目前为止所知道的以及我所做的:

I suppose it should do justice to state what I think I know so far as well as what I've done:

1) 我创建了应用程序并进行了第一次数据库迁移;我现在有我的开发、测试和生产数据库.开发数据库有一个名为wines"的表.

1) I created the app and did my first db migration; I now have my dev, test and production databases. The dev db has a table called 'wines'.

2) 我做了一个脚手架来创建必要的文件.

2) I made a scaffold which created the necessary files.

3) 基本的索引/更新/销毁方法都设置好了,我可以浏览页面了.

3) The basic index/update/destroy methods are set up and I can browse the pages.

4) 根据我收集的信息,ActiveRecord 类Wine"会自动从数据库继承属性?每一列都是一个属性,'wines' 表中的每一行都是一个潜在的实例化对象,它从 wine_controller 脚本中调用.

4) From what I gather, the ActiveRecord class "Wine" automatically inherits properties from the database? Each column is a property and each row in the table 'wines' is a potentially instantiated object which is called from the wine_controller script.

我现在遇到的问题是我想创建一个所有控制器都使用的通用布局.唯一会改变的是页面标题,可能是一些 <link>标头中的标签,<body>属性(最有可能是 javascript onload 事件)以及 <body> 中的任何内容.标签.

The problem I'm having now is that I want to create a common layout that all controllers use. The only things that will change will be the page title, potentially some <link> tags in the header, the <body> attributes (javascript onload events most likely) and whatever lies inside the <body> tag.

我发现自己在查找可以执行我想要的功能的函数(例如favicon_link_tag"、stylesheet_link_tag"和auto_discovery_link_tag"...)但我找不到放置它们的正确位置!我知道这与我对事物如何执行/继承缺乏了解有关.例如,如果我要在 application_controller.rb 中声明 @pageTitle 并在 ApplicationHelper 中使用 @pageTitle 它将不起作用.甚至在 application_controller.rb 中使用stylesheet_link_tag"也会引发错误.我只是没有得到一些东西.

I find myself looking up functions that will do what I want (like "favicon_link_tag", "stylesheet_link_tag" and "auto_discovery_link_tag"...) but I can't find the right place to PUT them! I know this has something to do with my lack of understanding of how things are executed/inherited. For example if I were to declare @pageTitle in application_controller.rb and use @pageTitle in ApplicationHelper it won't work. Or even using "stylesheet_link_tag" in application_controller.rb throws an error. I'm just not getting something.

在按时间顺序执行、范围等方面,每件事与另一件事有何关联?

How does each thing relate to another in terms of chronological execution, scope, etc.?

推荐答案

在您的app/views"目录中有一个名为layouts"的文件夹.默认情况下,那里应该有一个application.html.erb"文件,但如果没有,您可以创建它.

In your "app/views" directory there is a folder called "layouts." By default there should be an "application.html.erb" file in there, but if there isn't you can create it.

您的应用程序"布局文件是任何视图使用的默认布局文件.但是,如果您希望特定控制器使用不同的视图,您可以覆盖它.请参阅此 railscast这个 也很有帮助.

Your "application" layout file is the default layout file used by any view. However, if you want a particular controller to use a different view, you can override this. See this railscast, and this one is helpful too.

要理解的主要内容是任何特定视图的内容都会出现在 yield 方法出现在您的应用程序布局中的任何位置.主要的 'yield' 块获取由您的控制器操作指定的视图文件,但您可以将任何视图中的任何内容标记为传递给另一个 yield 块.例如,您提供的标题"示例可以传递给应用程序布局的头部.有关详细示例,请参阅此 railscast.

The main thing to understand is the content from any particular view will show up wherever the yield method appears in your application layout. The main 'yield' block gets the view file specified by your controller action, but you can mark anything inside any view to be passed to another yield block instead. For instance, the "title" example you gave could be passed to the head of your application layout. See this railscast for a detailed example of that.

有关更多信息,您应该阅读Rails 指南,您可能需要考虑选择上一本 Rails 入门书.

For more, you should read the Rails Guide, and you might want to consider picking up a Rails starter book.

我被Beginning Rails 3"弄湿了,这是一个对框架的惊人介绍.看了那本书几天,这对我来说很有意义,而且我的发展速度比以往任何时候都快.了解 Rails 后,它会动摇,但绝对值得一读.

I got my feet wet with "Beginning Rails 3," which was a phenomenal introduction to the framework. A couple days with that book and it was all making sense to me, and I was developing faster than I ever had before. Rails rocks once you get to know it, but it's definitely worth going through a book.

请继续提问,如果可以,我会提供帮助:)

Please continue to ask questions, I'll help if I can :)

-EDIT- 要回答您关于控制流的问题,它的工作原理基本上是这样的:

-EDIT- To answer your question about control flow, it basically works like this:

  1. 您的浏览器向特定 URL 发送 GET 请求.

  1. Your browser sends a GET request for a particular URL.

路由器接受该请求,将其与控制器动作相匹配,触发该控制器动作,并向控制器提供与该请求相关的任何参数.例如:如果您请求 example.com/posts/123?color=red 这将触发您的 posts_controllerSHOW 操作,并且将 {:color => 'red'} 传递给 params 哈希.您可以使用 params[:color]

The router takes that request, matches it to a controller action, triggers that controller action, and provides the controller any parameters associated with the request. For instance: if you requested example.com/posts/123?color=red this would trigger the SHOW action of your posts_controller, and would pass {:color => 'red'} to the params hash. You would access that using params[:color]

控制器动作做它的事情,当它完成时它呈现输出.默认情况下,它呈现位于 app// 中的任何视图,并且将匹配适合请求的扩展名的任何文件(即 AJAX 请求将触发 <;action_name>.js.erb 和 GET 请求将触发 .html.erb.

The controller action does its thing, and when it's done it renders output. By default it renders whatever view is located in app/<controller_name>/<action_name>, and will whichever file matches the extension appropriate to the request (ie an AJAX request would trigger <action_name>.js.erb and a GET request would trigger <action_name>.html.erb.

  • 您可以使用 render 方法覆盖它,例如通过传递 render 'foo/bar' 来使用 FooController 的视图进行渲染,而不是 Bar 动作您当前的操作.

  • You can override this using the render method, for example by passing render 'foo/bar' to render using the view for FooController, Bar action instead of your current action.

请注意,无论您渲染什么,视图可用的数据都是路由器触发的特定控制器操作中的任何内容,而不是正常"渲染该视图的控制器操作.

Note that no matter what you render, the data available to the view is whatever is in the specific controller action the router triggered, not the controller action that would 'normally' render that view.

使用来自调用它的控制器的数据解析视图文件.如果您有任何 content_for 方法,则 content_for 块内的视图代码将转到您告诉它的位置,否则其他所有内容都将转到您的 content_for 块中的主 YIELD 块code>application 布局(或您的控制器指定的任何布局).

The view file is parsed using the data from the controller that called it. If you have any content_for methods then the view code that is inside the content_for block will go where you tell it, otherwise everything else will go to the main YIELD block in your application layout (or whatever layout your controller specified instead).

解析应用程序布局,并将视图中的内容插入适当的区域.

The application layout is parsed, and the content from the view is inserted into the appropriate areas.

页面提供给用户.

这在某些方面是一种简化,但我认为它可以回答您的问题.再次,随时问:)

That's a simplification in some ways, but I think it answers your question. Again, feel free to keep asking :)

这篇关于仍然很难使用 RoR MVC 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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