SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2004
    Location
    Holland
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Instance variables in Controller and View

    Say I have something like this:

    Code:
    class FooController < ApplicationController
    
    @bars = bar.find_all
    
    end
    And then use it in the foo.rhtml view, like:

    Code:
    <% @bars.each do |bar| %>
    <%= bar.field %>
    <% end %>
    How is the data from the @bars instance available to the view?

    Thanks

  2. #2
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ruby magic

  3. #3
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, magic is one way of putting it, but I have a feeling you are looking for a more logical answer. Basically, the idea of creating a view and a controller is so that you can have a seperation between display and code. The view is for your display, but as far as most of your coding is concerned, it goes in the controller. In rails, the controller interacts with the view, so if you set an instance variable in your controller, you can access that variable in your view!

    I hope that makes sense. I think I confused myself. Basically, the 'controller' bone's connected to the 'view' bone. :-P

    ~Patrick Smith

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's like going back to the dark ages isn't it.......
    all down to magic

  5. #5
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Honestly, I haven't looked at the source for RoR. However, if I had to guess how this type of "magic" is achieved, I would think that the controller uses introspection to gather references to all its instance variables. Then when the view object is instanciated, the controller--or some other object--passes in an array of the controller's instance variables to the views constructor.

    Maybe some of the folks who are more in the know as far as how the internals of RoR can give a more accurate answer.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I wasn't having a pop, I think you are right... the template file isn't displayed directly but parsed. During this process its variables can be matched to those in the controller.

  7. #7
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when a render is called, the variables are assigned to the view in the controller.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •