我正在try 路由和命名空间.

当使用<%= 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_user_addresses GET    /customer/users/:user_id/addresses(.:format)                                                      customer/addresses#index
                                         POST   /customer/users/:user_id/addresses(.:format)                                                      customer/addresses#create
               new_customer_user_address GET    /customer/users/:user_id/addresses/new(.:format)                                                  customer/addresses#new
              edit_customer_user_address GET    /customer/users/:user_id/addresses/:id/edit(.:format)                                             customer/addresses#edit
                   customer_user_address GET    /customer/users/:user_id/addresses/:id(.:format)                                                  customer/addresses#show
                                         PATCH  /customer/users/:user_id/addresses/:id(.:format)                                                  customer/addresses#update
                                         PUT    /customer/users/:user_id/addresses/:id(.:format)                                                  customer/addresses#update
                                         DELETE /customer/users/:user_id/addresses/:id(.:format)                                                  customer/addresses#destroy

This is what i was able to do

  • 通过创建Customer::UsersController,我能够访问用户资源的所有crud操作,
  • 通过创建Customer::ContactsController,我能够访问所有crud操作,但我不得不添加resolve指令,以使路由生成正确
  resolve('Customer::Contact') {
    [:customer, :user, :contact]
  }

This is where i'm in trouble

当实现Customer::AddressesController时,我无法找到一种方法来使form_请求正确的路径.

这是控制器的动作

  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'.

为了使它工作,我必须传递一个正确的路径:<%= form_with model: [@user, @address], url: customer_user_addresses_path(@user) do |form| %>.

我错过了什么约定?有什么方法可以忽略URL参数?

Info

存储库:https://github.com/LuigiDeCosmis/GestionaleTest

要判断的模板位于customers/addresses/new. html. erb下

推荐答案

Where did 100 prefix come from?

您的错误消息中的useraddress都有customer_前缀

undefined method 'customer_user_customer_addresses_path'

因为您试图基于模型构建路径—而两个User/Address模型都位于Customer命名空间中.

具体来说,customer_前缀来自这里:

Customer::User.model_name.param_key
# => "customer_user"
Customer::Address.model_name.param_key
# => "customer_address"

这是它试图解析它的方式: https://github.com/rails/rails/blob/af773ae44a1ae465cc0d19b1e8bf3fc731221ef0/actionview/lib/action_view/helpers/form_helper.rb#L756

What convention am i missing?

我看不出有什么理由要像这样命名模型.

如果你希望你的用户是customer/admin或其他任何东西—你应该使用STI方法

https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html

https://medium.com/nyc-ruby-on-rails/single-table-inheritance-sti-ruby-on-rails-basics-2a08c81e50af

Is there any way to omit the url parameter?

是的在你摆脱了Customer个模型命名空间之后,你将能够在你的路由中看到类似的东西:

customer_user_addresses

这是一个可以使用模型选项设置的路由示例.

希望这对你有帮助!祝你好运!

Ruby-on-rails相关问答推荐

Rails 7 Active Record::SessionStore -如何获取控制器中当前会话的数据库ID?

未捕获语法错误:try 编辑TRIX时,请求的模块未在Rails 7.1中提供名为默认的导出(在youtube.js:1:8)

如何将 ActionController::RoutingError 分类为 Sentry 中的错误

为什么有时会出现类型错误没有将 StringIO 隐式转换为 String?Ruby /导轨

Gem::Ext::BuildError:ERROR:无法在macOS Monterey上构建Gem本机扩展

Rails ActiveRecord 中的更新插入

Rails 3 - Select 包含?

如何在 to_json 中获取回形针图像的 url

基于 2 列定义唯一主键

使用 SQLite 3 将 RoR 应用程序部署到 Heroku 失败

如何在 rspec 中运行单个测试?

Rails,获取模型中的资源路径

使用 oauth 和 twitter ruby​​ gem 时不断收到 OAuth::Unauthorized 错误

如何使用 Ruby 在现有 PDF 上编辑或书写?

验证一个对象是否有一个或多个关联对象

默认呈现 JSON 而不是 HTML?

过滤器在渲染之前但在控制器之后执行?

如何打印出范围之间的随机数?

活动管理员:仅自定义新表单

如何在 Rails 中设置路由的默认格式?