繁体   English   中英

奇怪的Rails路由错误

[英]Odd Rails Routing errors

尝试通过典型的rails表单创建新资源时,我得到了undefined method stripe_managed_accounts_path 下面是我的代码,我很傻,无法弄清楚。

控制者

class StripeManagedAccountsController < ApplicationController
  before_action :authenticate_printer!

  def new
    @stripe_managed_account = StripeManagedAccount.new(printer_id: current_printer.id)
  end
end

模型

class StripeManagedAccount < ActiveRecord::Base
    belongs_to :printer
end

视图/新

<h1>Create New Stripe Managed Account</h1>

<%= render 'form' %>

查看/表格

<h5>inside the form</h5>

<%= form_for @stripe_managed_account do |f| %>

<% end %>

路线

resources :printers, only: [:show, :edit, :update] do
    resources :stripe_managed_accounts
end

错误

`undefined method 'stripe_managed_accounts_path' for #<#<Class:0x007fc627d342b8>:0x007fc62b36e108>`

路线

printer_stripe_managed_accounts GET    /printers/:printer_id/stripe_managed_accounts(.:format)          stripe_managed_accounts#index
                                    POST   /printers/:printer_id/stripe_managed_accounts(.:format)          stripe_managed_accounts#create
 new_printer_stripe_managed_account GET    /printers/:printer_id/stripe_managed_accounts/new(.:format)      stripe_managed_accounts#new
edit_printer_stripe_managed_account GET    /printers/:printer_id/stripe_managed_accounts/:id/edit(.:format) stripe_managed_accounts#edit
     printer_stripe_managed_account GET    /printers/:printer_id/stripe_managed_accounts/:id(.:format)      stripe_managed_accounts#show
                                    PATCH  /printers/:printer_id/stripe_managed_accounts/:id(.:format)      stripe_managed_accounts#update
                                    PUT    /printers/:printer_id/stripe_managed_accounts/:id(.:format)      stripe_managed_accounts#update
                                    DELETE /printers/:printer_id/stripe_managed_accounts/:id(.:format)      stripe_managed_accounts#destroy

并且突出显示此行<%= form_for @stripe_managed_account do |f| %> <%= form_for @stripe_managed_account do |f| %>

我已经stripe_managed_accounts_path的整个代码库,但是没有地方。 我很奇怪...

更新:::

如果我添加那条路线,它会消失吗??? 为什么要寻找那条路线。 是因为我如何命名婴儿用品等?

您要将stripe_managed_accounts嵌套在路由文件上的printers 如果查看一下rake routes的输出,您会发现stripe_managed_accounts_path没有路径。

您可以在stripe_managed_accounts资源上使用浅选项,也可以调整表格以包括托管帐户将属于的打印机。

#controller
class StripeManagedAccountsController < ApplicationController
  before_action :authenticate_printer!

  def new
    @stripe_managed_account = current_printer.build_stripe_managed_account
  end

  def create
    current_printer.stripe_managed_accounts.create stripe_managed_account_params
    # handle response
  end

  def stripe_managed_account_params
    params.require(:stripe_managed_account).permit([list of attributes])
  end
end

#form
<h5>inside the form</h5>

<%= form_for [current_printer, @stripe_managed_account] do |f| %>

<% end %>

这将生成正确的URL,将stripe_managed_account嵌套在当前打印机中。

有关has_one关联的信息,请参见http://guides.rubyonrails.org/association_basics.html#has-one-association-reference

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM