Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources Routes to Update Actions An Edit Route

Brian Gilbank
Brian Gilbank
9,586 Points

Link to show action

Quick question: how do I link to a show action by id in my header menu? I have created a show page called about using the friendly_id gem. Pages was created with the scaffold command.

How do I then link to that specific show page in the main header menu?

show page:

currently have: <%= link_to "About", pages_path, :action => "show", :id => 'about' %> but that doesn't work?

page model:

extend FriendlyId
  friendly_id :title, use: :slugged
end

pages_controller.rb:

def show
    @page = Page.friendly.find(params[:id])
end

routes.rb:

resources :pages
get '/about', to: 'pages#show', :via => [:get, :post], :defaults => {:id => 'about'}
routes.rb
Rails.application.routes.draw do
  get '/pets/new', to: 'pets#new'
  get '/pets/:id', to: 'pets#show'
  post '/pets', to: 'pets#create'
end

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

I haven't used friendly_id before, although I have a pretty good guess how it works...

In this code:

<%= link_to "About", pages_path, :action => "show", :id => 'about' %>

The second argument should be a string with the path that the final link will point to. The pages_path method should return "/pages", but I think you actually want "/about". Since you haven't named the get "/about" route, I think you'll need to use the literal string "/about".

Side note regarding this route:

get '/about', to: 'pages#show', :via => [:get, :post], :defaults => {:id => 'about'}

You should probably take the :via option out. I don't think you want POST requests going to the show action, and :via => [:get] is redundant with calling the get method anyway.

Brian Gilbank
Brian Gilbank
9,586 Points

Ah thank you! I was over complicating it.