SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Linking to Home

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Linking to Home

    I'm having the darnedest time understanding something I'm sure is very simple (at least was under other web dev platforms where I would do a href...C:\windows\apps\home.html). I'm trying to set up a link to my home page

    I have an application controller:
    Code:
    # Filters added to this controller apply to all controllers in the application.
    # Likewise, all the methods added will be available for all controllers.
    
    class ApplicationController < ActionController::Base
      layout "main" 
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details
    
      # Scrub sensitive parameters from your log
      # filter_parameter_logging :password
    end
    And a home controller:
    Code:
    class HomeController < ApplicationController
      def index
      end
    
    end
    and a main.html.erb:
    Code:
    !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
      <title>Pics4Flicks</title>
      <%= stylesheet_link_tag 'style' %>
    </head>
      <body>
        <div id="container">
      <div id="top">
      </div>
      <div id="navbar">
      <ul>
        <li>Home</li>
        <li><%= link_to 'Movies', movies_path %></li> #this link works fine
        <li>DVD</li>
        <li>News</li>
        <li>Forum</li>
        <li>Contact</li>
      </ul>
    </div>
    I would like home to display the contents of my main.html.erb file. (or should it be application.html.erb?). To further confuse me, my home folder, under /apps/views/home has an index.html.erb file.

    I must be mixing up my views (have I somehow created too many?) and perhaps not defining things properly in routes.rb. Hopefully haven't confused you all as much as I have myself. Any help most appreciated.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The HomeController is going to display the views contained within the app/views/home folder. index.html.erb is the default view that will be displayed when the url looks like this: localhost:3000/home.

    If you want to define a default controller/view for your app you would want to put something like this in your routes.rb:
    Code:
      map.root :controller => "home", :action => "index"
    If you do this, make sure to delete the index.html file in the public folder.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scannon View Post
    The HomeController is going to display the views contained within the app/views/home folder. index.html.erb is the default view that will be displayed when the url looks like this: localhost:3000/home.

    If you want to define a default controller/view for your app you would want to put something like this in your routes.rb:
    Code:
      map.root :controller => "home", :action => "index"
    If you do this, make sure to delete the index.html file in the public folder.
    Thanks!!

    Ok, that got my main view to appear, but minus some of the html. It displays the header and footer, but none of the other div content. Do I have another file/association mixup of some type?

    and assuming main.html.erb is the layout template for the entire site, (not application.html.erb), how to I properly format the link_to statement?
    Code:
    <li><%= link_to 'Home', home_path %></li>

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    99
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Really you should just get rid of the "main.html.erb" file. I don't see any reason for you to need that. (Perhaps you can explain what the purpose behind main.html.erb is?)

    If you want a default layout for your entire application you need to use application.html.erb. It will need to look something like this:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title><&#37;= h(yield(:title) || "Untitled") %></title>
        <%= stylesheet_link_tag 'application' %>
        <%= javascript_include_tag :defaults %>
        <%= yield(:head) %>
      </head>
      <body>
        <div id="container">
        <div id="header"><h1>Roundtable</h1></div>
        <div id="wrapper">
          <div id="content">
            <%- flash.each do |name, msg| -%>
              <%= content_tag :div, msg, :id => "flash_#{name}" %>
            <%- end -%>
          
            <%- if show_title? -%>
              <h1><%=h yield(:title) %></h1>
            <%- end -%>
          
            <%= yield %>
          </div>
        </div>
        <div id="navigation">
          <strong>Navigation</strong>
        </div>
        <div id="footer">Footer.</div>
        </div>
      </body>
    </html>
    There are a few things that you need to note here:

    First, you can include the stylesheet, javascript, or any other necessities right here and then you do not have to worry about them again.

    Second, this is where you can implement "the flash" for passing messages from controllers.

    Third, you use <%= yield %> where you want the content of your controllers to be displayed. For now, ignore the other yield tags that I have in this example.

    Fourth, your views for your controllers will look something like the following:
    Code:
      <% for movie in @movies %>
            <h3><%=h movie.title %></h3>
            <p>
                 <%=h movie.description %>
            </p>
        <% end %>
    That code will be inserted into the application.html.erb and the page will be rendered to the browser.

    Designing your app like this may save you some frustrations. Hope this helps.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scannon View Post
    Really you should just get rid of the "main.html.erb" file. I don't see any reason for you to need that. (Perhaps you can explain what the purpose behind main.html.erb is?)

    If you want a default layout for your entire application you need to use application.html.erb. It will need to look something like this:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title><%= h(yield(:title) || "Untitled") %></title>
        <%= stylesheet_link_tag 'application' %>
        <%= javascript_include_tag :defaults %>
        <%= yield(:head) %>
      </head>
      <body>
        <div id="container">
        <div id="header"><h1>Roundtable</h1></div>
        <div id="wrapper">
          <div id="content">
            <%- flash.each do |name, msg| -%>
              <%= content_tag :div, msg, :id => "flash_#{name}" %>
            <%- end -%>
          
            <%- if show_title? -%>
              <h1><%=h yield(:title) %></h1>
            <%- end -%>
          
            <%= yield %>
          </div>
        </div>
        <div id="navigation">
          <strong>Navigation</strong>
        </div>
        <div id="footer">Footer.</div>
        </div>
      </body>
    </html>
    There are a few things that you need to note here:

    First, you can include the stylesheet, javascript, or any other necessities right here and then you do not have to worry about them again.

    Second, this is where you can implement "the flash" for passing messages from controllers.

    Third, you use <%= yield %> where you want the content of your controllers to be displayed. For now, ignore the other yield tags that I have in this example.

    Fourth, your views for your controllers will look something like the following:
    Code:
      <% for movie in @movies %>
            <h3><%=h movie.title %></h3>
            <p>
                 <%=h movie.description %>
            </p>
        <% end %>
    That code will be inserted into the application.html.erb and the page will be rendered to the browser.

    Designing your app like this may save you some frustrations. Hope this helps.
    I think I'd created main.html.erb after reading one too many tutorials :-) In any case, I did delete it, copying that code to application.html.erb. How do I format the link_to code for the Home button on my navigation?

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This seemed to work, is it the proper convention though?

    Code:
    <li><%= link_to 'Home', '/' %></li>
    <li><%= link_to 'Movies', movies_path %></li>
    ...

  7. #7
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You can use root_path instead of '/'. Though running the app in a subdirectory
    is quite uncommon.


    'rake routes' on your terminal will tell you all route helpers you can use in your views.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomK32 View Post
    Hi,

    You can use root_path instead of '/'. Though running the app in a subdirectory
    is quite uncommon.


    'rake routes' on your terminal will tell you all route helpers you can use in your views.
    running the app in a subdirectory... - not sure I understand exactly what this means. (yes, this is my first app, trying to get my arms around things).

  9. #9
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then don't think about ;-)

    You can like add a whole blogging app and run it in http://example.com/blog/ by setting ActionController::AbstractRequest.relative_url_root where a link to '/' would send the user to a wrong place outside the blogging app.

    But well, as it's your first app don't bother about too much.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomK32 View Post
    Then don't think about ;-)

    You can like add a whole blogging app and run it in http://example.com/blog/ by setting ActionController::AbstractRequest.relative_url_root where a link to '/' would send the user to a wrong place outside the blogging app.

    But well, as it's your first app don't bother about too much.
    Great, will concentrate on the next step then...rss feeds. off to do more googling

    Thanks to everyone for all the help!! Scary, I'm actually starting to understand some of this stuff.


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
  •