在尝试通过典型的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| %>
我已经为stripe_managed_accounts_path
编写了整个代码库,但它不在哪里。我很有可能...
UPDATE :::
如果我添加该路线它会消失???为什么要寻找那条路线。是因为我如何命名我的fodlers等等?
答案 0 :(得分:1)
您在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 %>
这将生成正确的网址,将stripe_managed_account
嵌套在当前打印机内。
对于has_one关联参考http://guides.rubyonrails.org/association_basics.html#has-one-association-reference