开发者

How can I correctly link to a static page?

开发者 https://www.devze.com 2023-03-12 19:11 出处:网络
I\'ve followed a railscast on creating and displaying static pages using a pages model and my code looks like this:

I've followed a railscast on creating and displaying static pages using a pages model and my code looks like this:

Pages model

has fields of name, permalink and description.

Routes:

get "log_in" => "sessions#new", :as => "log_in"
get "log_out" => "sessions#destroy", :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"

resources :pages

match ':permalink', :controller => 'pages', :action => 'show'

_footer.html.erb

<div class="footer">
  <div class="footer_container">
    <%= link_to 'About Us', root_path('about') %>
  </div>
</div>

going to /localhost:3000/about displays the about page correctly but the link in the footer wants to dire开发者_运维技巧ct to /localhost:3000/.about and actually links to the sign up a new user page.

How can I get my link to direct to /about and display the page?

Thanks for any help its much appreciated!


root_path will always take you to users#new because that is what you specified in your routes.rb file. What you can do is name your last route with the :as key, like this:

match ':permalink', :controller => 'pages', :action => 'show', :as => 'my_page'

Then in your views, you should be able to do something like this:

<%= link_to 'About Us', my_page_path('about') %>
0

精彩评论

暂无评论...
验证码 换一张
取 消