首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇数Rails路由错误

奇数Rails路由错误
EN

Stack Overflow用户
提问于 2015-12-28 07:51:59
回答 1查看 35关注 0票数 0

当我试图通过典型的rails表单创建一个新资源时,我得到了一个undefined method stripe_managed_accounts_path。下面是我的代码,我被搞糊涂了,弄不明白。

控制器

代码语言:javascript
复制
class StripeManagedAccountsController < ApplicationController
  before_action :authenticate_printer!

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

模型

代码语言:javascript
复制
class StripeManagedAccount < ActiveRecord::Base
    belongs_to :printer
end

视图/新视图

代码语言:javascript
复制
<h1>Create New Stripe Managed Account</h1>

<%= render 'form' %>

视图/表单

代码语言:javascript
复制
<h5>inside the form</h5>

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

<% end %>

路线

代码语言:javascript
复制
resources :printers, only: [:show, :edit, :update] do
    resources :stripe_managed_accounts
end

错误

代码语言:javascript
复制
`undefined method 'stripe_managed_accounts_path' for #<#<Class:0x007fc627d342b8>:0x007fc62b36e108>`

路线

代码语言:javascript
复制
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的整个代码库都很感兴趣,但它并不在任何地方。我的意见很不一致...

更新:

如果我添加该路由,它将消失?为什么它要寻找那条路线。是不是因为我给我的牧羊人起的名字,等等?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-28 09:19:18

您在路由文件中的printers中嵌套了stripe_managed_accounts。如果查看一下rake routes的输出,就会发现没有通向stripe_managed_accounts_path的路径。

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

代码语言:javascript
复制
#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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34486122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档