我正在尝试路由和命名空间。
使用
<%= form_with model: [@user, @address] do |form| %>
实现表单时
我收到此错误undefined method 'customer_user_customer_addresses_path'
这是路由架构
namespace :customer do
resources :users do
resource :contact
resources :addresses
end
end
resolve('Customer::Contact') {
[:customer, :user, :contact]
}
rails routes
命令打印的所有路线均符合预期。
Customer::UsersController
我能够访问用户资源的所有 CRUD 操作Customer::ContactsController
我能够访问所有 CRUD 操作,但我必须添加解析指令才能正确生成路由 resolve('Customer::Contact') {
[:customer, :user, :contact]
}
在实现
Customer::AddressesController
时,我无法找到一种方法来使form_with请求正确的路径。
这是控制器动作
def new
@breadcrumbs = []
@user = Customer::User.find(params[:user_id])
@address = @user.addresses.build
end
这是新模板
<%= form_with model: [@user, @address] do |form| %>
当我导航到新操作时,我收到此错误
undefined method 'customer_user_customer_addresses_path'
为了使其正常工作,我必须传递带有正确路径的 url 参数:
<%= form_with model: [@user, @address], url: customer_user_addresses_path(@user) do |form| %>
。
我缺少什么约定?有没有办法省略url参数?
将
customer
命名空间添加到 model
参数可能会有所帮助:
<%= form_with model: [:customer, @user, @address] do |form| %>
https://guides.rubyonrails.org/form_helpers.html#dealing-with-namespaces