SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Linking to Home
-
Jun 6, 2009, 13:24 #1
- 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
Code:class HomeController < ApplicationController def index end end
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 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.
-
Jun 6, 2009, 19:04 #2
- Join Date
- Feb 2003
- Location
- Akron, OH, USA
- Posts
- 106
- 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"
-
Jun 6, 2009, 20:10 #3
- Join Date
- Mar 2009
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>
-
Jun 7, 2009, 05:53 #4
- Join Date
- Feb 2003
- Location
- Akron, OH, USA
- Posts
- 106
- 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><%= 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>
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 %>
Designing your app like this may save you some frustrations. Hope this helps.
-
Jun 7, 2009, 10:17 #5
- Join Date
- Mar 2009
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 7, 2009, 11:01 #6
- 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> ...
-
Jun 8, 2009, 05:43 #7
- 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.
-
Jun 8, 2009, 06:49 #8
- Join Date
- Mar 2009
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 8, 2009, 06:59 #9
- 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.
-
Jun 8, 2009, 08:07 #10
- Join Date
- Mar 2009
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks