2015-12-26 95 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.