开发者

Still having a hard time with RoR MVC approach

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

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) I made a scaffold which created the necessary files.

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

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.

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.

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/i开发者_高级运维nherited. 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.?


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.

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.

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.

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

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- To answer your question about control flow, it basically works like this:

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

  2. 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]

  3. 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.

    • 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.

  4. 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).

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

  6. The page is served to the user.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜