一個簡單的辦法讓你的控制器的工作是在你的路由添加資源。這將映射您的所有控制器方法。
# app/controllers/controllernames_controller.rb
class ControllernamesController < ApplicationController
def index
end
# and other methods you want...
end
# config/routes.rb
MyApp::Application.routes.draw do
resources :controllername
end
那麼你的鏈接將成爲http://localhost/controllernames/methodname/id
然後在視圖文件,你可以添加鏈接的方式如下:
<%= link_to "whatever_your_like_to_name", controllernames_path %>
如果使用REST風格的腳手架Rails提供那麼你可以做:
<%= link_to "whatever_your_like_to_name", new_controllername_path %>
# or
<%= link_to "whatever_your_like_to_name", edit_controllername_path(controllername) %>
# or
etc...
另一種從cont獲得單一方法的方法滾你可以做以下的路線:
# config/routes.rb
MyApp::Application.routes.draw do
get 'controllernames/methodname', to: 'controllernames#methodname', as: "whatever_you_want"
end
在這種情況下,你在視圖文件鏈接將
<%= link_to "whatever_you_like_to_name", whatever_you_want_path %>
現在我有:'的ActionController :: RoutingError(無路由匹配「/後/ index'' 另外我試過這個:'map.connect'post/index',:controller =>'post',:action =>'index'',但是在瀏覽器中是404而在日誌中是200。 – droptheplot
好吧,不知道爲什麼,但現在它工作。謝謝! – droptheplot