4 Replies - 1759 Views - Last Post: 24 November 2012 - 07:05 AM Rate Topic: -----

#1 tools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 26-September 12

RoR: Problems with routing

Posted 24 November 2012 - 03:50 AM

I have the following problem:
I have route root to pages#index, but that kinda messed up my navigation, so that when I pres for instace about I get No route matches [GET] "/about"
and since I am new to rails I kinda need at bit of help, because I have tried finding a solution at stackoverflow and google eith out success.
My navigation menu is like this:
<nav>
		<b><%= link_to "INDEX", 'index' %></b>
		<b><%= link_to "BLOG", '/blog' %></b>
                <b><%= link_to "PORTFOLIO", 'portfolio' %></b>
		<b><%= link_to "ME", 'me' %></b>
		<b><%= link_to "ABOUT", 'about' %></b>
</nav>



My routing to index is like this in config/routes.db:
WebApp::Application.routes.draw do
    
  resources :posts

  get "pages/index"

  get "pages/blog"

  get "pages/portfolio"

  get "pages/me"

  get "pages/about"

  root :to => "pages#index"

 
end



What i thought I should do and what I found on stackoverflow was to replace for instance:
get "pages/blog"

With:
match '/blog', to: 'pages#blog'

But when I do this happens:
/Users/tools/h4xcode/web_app/config/routes.rb:8: syntax error, unexpected ':', expecting kEND
    match '/blog', to: 'pages#blog'

Can you give me a nock in the right direction or perhaps even a soloution?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: RoR: Problems with routing

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,266
  • Joined: 21-June 11

Re: RoR: Problems with routing

Posted 24 November 2012 - 05:47 AM

Your link is linking to a page called "about". Since this doesn't start with a slash (or a protocol), it's a relative URI, that is it's relative to the "directory" you're currently in. Before you changed your root route, you'd always be in the /pages "directory" when clicking the about link, so it would link to /pages/about.

But now you're accessing the link from /, so it links to /about, not /pages/about. And as Rails correctly pointed out, there is no route for the URI /about.

You can fix this by making /about map to Pages#about like you're trying to, but then the link will only work if you're in the / "directory". So if you're ever on an URI like /posts/123, the link will break again because now it's trying to link to /blog/about.

The proper fix would be to generate links using an absolute URI rather than a relative one. Rails automatically generates helper methods for you that give you absolute links to the given route. for example the pages_about_path method would give you an absolute path for your get 'pages/about' route.

The syntax error you're getting is probably because you're still running Ruby 1.8, but trying to use the new 1.9 Hash syntax. On 1.8 use :key => value, not key: value to pass "keyword arguments".
Was This Post Helpful? 0
  • +
  • -

#3 tools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 26-September 12

Re: RoR: Problems with routing

Posted 24 November 2012 - 06:34 AM

So what you say is that I could do this:
<b><%= link_to "BLOG", 'pages_blog_path' %></b>

This post has been edited by tools: 24 November 2012 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,266
  • Joined: 21-June 11

Re: RoR: Problems with routing

Posted 24 November 2012 - 06:52 AM

Yes, but without the quotes. You don't want to link to "pages_blog_path" - you want to link to whichever string the method pages_blog_path returns.
Was This Post Helpful? 0
  • +
  • -

#5 tools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 26-September 12

Re: RoR: Problems with routing

Posted 24 November 2012 - 07:05 AM

Thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1