SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails: Layouts & Search/Sort

    Three questions for you all...
    1.
    Code:
      def index
        list
        render :action => 'list'
      end
    Why the need for 'list' above the render?

    2. I have an admin section to a site I'm building. Here's the pertinent
    bit of the layout that all controllers are using:
    Code:
    <div id="main-content">
      <%= @content_for_layout %>
    </div>
    <div id="sidebar">
    </div>
    I'm using @content_for_layout to display various controller actions such
    as new, edit, list etc. My problem is that for each controller I want a
    specific chunk of content to show up in the sidebar. How can I
    accomplish this without altering the layout to look something like this:
    Code:
    <div id="main-content">
      <%= @content_for_layout %>
    </div>
    <div id="sidebar">
      <% if @controller.controller_name == 'news' %><%= render(:partial => 
    "news_sidebar") %><% end %>
      <% if @controller.controller_name == 'users' %><%= render(:partial => 
    "users_sidebar") %><% end %>
      <% if @controller.controller_name == 'links' %><%= render(:partial => 
    "links_sidebar") %><% end %>
    </div>
    ...There has to be a much more DRY way of doing that. Perhaps a helper
    that takes the controller name as an argument? Other suggestions?

    3. I'm used to building searches and sorting mechanisms with PHP that
    analyze a $_GET request and display data based on those parametrs. URLs
    end up looking like:
    Code:
    http://example.com/search.php&order=desc&category=foo&author=fred
    I have no idea how to build something simple like this with Rails -- can
    someone provide some code for me to look at that does just this? I'm
    very much a learn by seeing kind of guy and would greatly appreciate it.

    Thanks.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    For #2, you can do something like this:
    Code:
    <% current_sidebar = controller.controller_name + "_sidebar" %>
    <%= render :partial => current_sidebar %>
    Less code at least.

    For #3, the params[] hash is the equivalent of $_GET. Or are you talking about how you'd construct that query?

  3. #3
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    For #3, the params[] hash is the equivalent of $_GET. Or are you talking about how you'd construct that query?
    ...I suppose I'd like to know what the form XHTML should look like, how the query will be constructed in the controller and how it will be handled by the model. Thanks!

  4. #4
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #1, index is an alias for the list action; the solitary "list" is a call to the list method, followed by a call to render the view for the list action.

  5. #5
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees
    the solitary "list" is a call to the list method
    ...perfect. Thanks.

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For your sidebar content, you could use content_for:

    Code:
    # layout file
    <div id="sidebar">
      <%= yield :sidebar %>
    </div>
    
    # in any of your views
    <% content_for(:sidebar) do %>
      everything in here will get inserted into
      your sidebar div
    <% end %>
    Also note that you should now be using <%= yield %> instead of <%= @content_for_layout %> as the latter is deprecated.

  7. #7
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    For your sidebar content, you could use content_for:

    Code:
    # layout file
    <div id="sidebar">
      <%= yield :sidebar %>
    </div>
    
    # in any of your views
    <% content_for(:sidebar) do %>
      everything in here will get inserted into
      your sidebar div
    <% end %>
    Also note that you should now be using <%= yield %> instead of <%= @content_for_layout %> as the latter is deprecated.
    Thanks Luke.


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
  •