简体   繁体   中英

In Rails 3, how do I get the path of a controller's `index` action?

In my routes.rb :

get 'contact/index'

If it matters, contact is a controller with only an index method; it does not have a corresponding model.

In my layout file:

<%= link_to 'Contact', contact_path %>

This is the error I'm getting:

undefined local variable or method `contact_path'

Do I have to give my route an alias? If so, how would I do that?

You need to make it a named route. For instance:

match 'contact' => 'contact#index', :as => :contact

I don't know if you actually want to have "index" in the URL, so I removed it. You also need to map your URL at the corresponding controller & action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM