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 trialBrian Gilbank
9,586 PointsLink 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'}
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
Treehouse TeacherI 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
9,586 PointsBrian Gilbank
9,586 PointsAh thank you! I was over complicating it.