gpt4 book ai didi

ruby-on-rails - Rails 路由命名空间和 form_for

转载 作者:行者123 更新时间:2023-12-03 05:16:31 26 4
gpt4 key购买 nike

我的routes.rb中有命名空间

  namespace :businesses do
resources :registration
end

我的 Controller 位于 services/registration_controller 子目录中。

def new
@business = Business.new
end

在我看来,我想这样做 form_for @business do |f| ... 但我收到以下错误:

没有路由匹配 {:controller=>"businesses", :action=>"create"}

重新启动服务器,我也得到了这个:

未定义方法businesses_path' for #<#:0x10339bb20>`

这是我的佣金路线:

                   home_index GET    /home/index(.:format)                       {:action=>"index", :controller=>"home"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
user_password PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
user PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
user DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
full_tree_admin_categories GET /admin/categories/full_tree(.:format) {:action=>"full_tree", :controller=>"admin/categories"}
admin_categories GET /admin/categories(.:format) {:action=>"index", :controller=>"admin/categories"}
admin_categories POST /admin/categories(.:format) {:action=>"create", :controller=>"admin/categories"}
new_admin_category GET /admin/categories/new(.:format) {:action=>"new", :controller=>"admin/categories"}
edit_admin_category GET /admin/categories/:id/edit(.:format) {:action=>"edit", :controller=>"admin/categories"}
admin_category GET /admin/categories/:id(.:format) {:action=>"show", :controller=>"admin/categories"}
admin_category PUT /admin/categories/:id(.:format) {:action=>"update", :controller=>"admin/categories"}
admin_category DELETE /admin/categories/:id(.:format) {:action=>"destroy", :controller=>"admin/categories"}
businesses_registration_index GET /businesses/registration(.:format) {:action=>"index", :controller=>"businesses/registration"}
businesses_registration_index POST /businesses/registration(.:format) {:action=>"create", :controller=>"businesses/registration"}
new_businesses_registration GET /businesses/registration/new(.:format) {:action=>"new", :controller=>"businesses/registration"}
edit_businesses_registration GET /businesses/registration/:id/edit(.:format) {:action=>"edit", :controller=>"businesses/registration"}
businesses_registration GET /businesses/registration/:id(.:format) {:action=>"show", :controller=>"businesses/registration"}
businesses_registration PUT /businesses/registration/:id(.:format) {:action=>"update", :controller=>"businesses/registration"}
businesses_registration DELETE /businesses/registration/:id(.:format) {:action=>"destroy", :controller=>"businesses/registration"}
root /(.:format) {:action=>"index", :controller=>"home"}

最佳答案

如果您有命名空间路由,最好的方法是:

class Admin::BusinessesController < ApplicationController
def new
@business = Business.new
end
end

在routes.rb中:

namespace :admin do
resources :businesses
end

查看中:

form_for [:admin, @business] do |f|...

文档:http://guides.rubyonrails.org/form_helpers.html第 2.3.1 节处理命名空间

关于您的情况:

在routes.rb中一切正常。在 View 中,您应该显式编写 url,因为 Controller 中除了 Controller 名称之外还有变量:

form_for @business, :url => business_registration_path do |f|...

我想在businesss/registration_controller中你有这样的东西:

class Businesses::RegistrationController < ApplicationController
def new
@business = Business.new
end
end

还有一点:我不会创建 Registration_controller 来注册新业务。我认为将业务相关的操作保留在business_controller中会更清晰。

关于ruby-on-rails - Rails 路由命名空间和 form_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3853531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com