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

gaicitadie · 2019年05月10日 · 最后由 razertory 回复于 2019年05月11日 · 1477 次阅读

在 routes.rb 里面,定义了

collection do
    get :new
    post :create
end

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

没办法,只能改成

collection do
    get :new1
    post :create1
end

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

1 楼 已删除

news

create_one

nine 回复

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

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

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

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

需要 登录 后方可回复, 如果你还没有账号请 注册新账号