Adn*_*Ali 41 ruby routes ruby-on-rails-3 ruby-on-rails-4
在Rails 3 Match中,用于指向"GET"和"POST"以及其他类型请求的操作.
match "user/account" => user#account
Run Code Online (Sandbox Code Playgroud)
现在,这将指向用户控制器对GET和POST请求的帐户操作.在Rails 4中, "匹配"已被弃用,我们可以在Rails 4中为GET和POST创建相同的路由吗?
Tim*_*orr 87
从match文档中,您可以使用match,只要您有via:
match "user/account" => "user#account", as: :user_account, via: [:get, :post]
Run Code Online (Sandbox Code Playgroud)
编辑:添加了一个as:参数,以便可以通过url帮助程序访问它.user_account_path或者user_account_url在这种情况下.
Ch *_*han 43
在路线上,匹配方法将不再作为全能选项.您现在应该使用以下选项指定要响应的HTTP谓词:via
match "/users/:id" => "users#show"
Run Code Online (Sandbox Code Playgroud)
match "/users/:id" => "users#show", via: :get
Run Code Online (Sandbox Code Playgroud)
match "/users" => "users#index", via: [:get, :post]
Run Code Online (Sandbox Code Playgroud)
更好的Rails 3.2兼容性的另一个选择是使用显式的get,post或任何其他HTTP谓词来指定您的操作.使用此选项,您仍然可以在今天运行代码,并在将来证明它可用于升级.
get "/users/:id" => "users#show"
Run Code Online (Sandbox Code Playgroud)
get "/users" => "users#index"
post "/users" => "users#index"
Run Code Online (Sandbox Code Playgroud)