2016-01-20 127 views
-1

我已经创建了一个控制器页面和一些简单页面的操作(例如联系我们),然后我去了routes.rb并创建了一个允许用户直接转到/ contactus的路径,而不是/页/联系我们。Link_to指向静态路由

如何将link_to指向动作,但仍然获得正确的路由url?

回答

0
#config/routes.rb 
resources :pages, path: "", only: [] do #-> has to be above everything in routes file 
    collection do 
    get :contact_us #-> url.com/contact_us 
    get :about  #-> url.com/about 
    end 
end 

root ... 

你会链接到它如下:

<%= link_to "Contact", pages_contact_us_path %> 
1
get :contact_us, to: 'pages#contact_us' 

get :contact_us, controller: :pages, action: :contact_us 

这将产生路径contact_us_path或URL contact_us_url

HEARE MORE ABOUT ROUTES IN RAILS

0

这是一个语法在pages控制器中使用contactus动作的简单路由:

get '/contactus' => 'pages#contactus'

,或者如果你想为你的路径更简单的名字:

get '/contactus' => 'pages#contactus', as: :contact