Rails 怎样让 new、create、update 这些 action 脱离 restful 的约束,像普通 action 一样使用?

gaicitadie · May 10, 2019 · Last by razertory replied at May 11, 2019 · 1477 hits

在 routes.rb 里面,定义了

collection do
    get :new
    post :create
end

会报错,可能是跟系统默认的 restful 约定有冲突。

没办法,只能改成

collection do
    get :new1
    post :create1
end

这样就没冲突了,但强迫症看着 new1、create1 这种命名不舒服。有没有解决这种冲突的办法?

1 Floor has deleted

news

create_one

Reply to nine

感谢回复,我还是用系统约定的 restful 吧

路由和 controller 的 action 之间可以分离的,不过 Rails 的确很少这样做。

Rails.application.routes.draw do
    get 'statement', to: 'static_pages#about'
end

兄弟,不用 restful 约束,后面代码很容易成屎山的

You need to Sign in before reply, if you don't have an account, please Sign up first.