如何正确地链接到静态页面?

时间:2022-01-19 19:33:00

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

我跟随railscast使用页面模型创建和显示静态页面,我的代码是这样的:

Pages model

页面模型

has fields of name, permalink and description.

具有名称、permalink和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

_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 direct to /localhost:3000/.about and actually links to the sign up a new user page.

进入/localhost:3000/about正确显示了about页面,但是页脚中的链接想要指向/localhost:3000/。about和实际链接到注册一个新的用户页面。

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

我怎样才能让我的链接指向和显示页面?

Thanks for any help its much appreciated!

非常感谢您的帮助!

1 个解决方案

#1


3  

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:

root_path将始终带您到users#new,因为这是您在路由中指定的。rb文件。你所能做的就是用“as key”来命名你最后的路线:

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') %>

#1


3  

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:

root_path将始终带您到users#new,因为这是您在路由中指定的。rb文件。你所能做的就是用“as key”来命名你最后的路线:

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') %>