2015-12-26 99 views
0

目前我的路由存在一些問題。Rails中的路由問題

我有一個功能添加控制器房子。我也有在房子/ add.html.erb視圖

我與域/房屋叫它/加那麼我得到這個錯誤:沒有路由匹配[GET]「/房/加」

路線。 RB是這樣的:

resources :api_users, :as => :users 

get '/:controller(/:action(/:id))' 
post '/:controller(/:action(/:id))' 

回答

1

如果您打算只使用get和post方法,由於內存使用情況,
請勿使用resources

match "houses/add" => "houses#add", via: [:get, :post]

,從來沒有使用routes.rb

get '#{action}' <- this is not working 

get "#{action" <- this works. 



    YOURCONTROLLER.action_methods.each do |action| 
    get "CONTROLLER_NAME/#{action}", to: "CONTROLLER_NAME##{action}" 
    end 
+0

單引號我必須添加每個方法現在一個新的路線? – Felix

+1

如果你的方法不遵循Rails路由約定,是的。 有簡單的解決方法。我更新了我的答案@Felix – seoyoochan

+0

通常他們這樣做。我認爲問題在於單引號。謝謝 – Felix

0

它改成這樣:

resources :api_users, as: :users 

# empty for memory concerns 
resources :houses, only: [] do 
    collection do 
     get :add 
     post :another_action 
    end 
end 

,或者如果您只是想重新命名新的補充,那麼你可以做這樣的事情:

resources :houses, path_names: { new: 'add' } 

# Which will now path /domain/houses/new --> /domain/houses/add 
# NOTE* This does not change the actual action name, it will still look for houses#new 

一些需要注意的有關match協議一條路由:

guides.rubyonrails.org/routing 3.7 HTTP動詞約束

一般情況下,你應該使用GET,POST,放,補丁和刪除方法約束到特定動詞的路線。您可以使用匹配方法與:通過選項來一次匹配多個動詞:

match 'photos', to: 'photos#show', via: [:get, :post] 

您可以通過使用匹配所有動詞特定的路線:所有:

match 'photos', to: 'photos#show', via: :all 

Routing both GET and POST requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.

'GET' in Rails won't check for CSRF token. You should never write to the database from 'GET' requests, for more information see the security guide on CSRF countermeasures.