3

在我的 rails 项目中,查看时出现以下错误/subscription/new

NoMethodError in Subscriptions#new

Showing /redacted/app/views/subscriptions/new.html.erb where line #4 raised:

undefined method `subscriptions_path' for #<#<Class:0x007fd02c8bbb28>:0x007fd0308f7a48>

Extracted source (around line #4):

1: <div class="grid_6">
2:   <h1>New Subscription</h1>
3:   <p>
4:     <%= form_for @subscription, :html => { class: 'form_dark' } do |f| %>
5:       <% if @subscription.errors.any? %>
6:         <div class="error_messages">
7:           <h1><%= pluralize(@subscription.errors.count, "error") %> prohibited this subscription from being saved:</h1>

我的路线文件包含resource :subscription此内容。

附加代码信息:

用户型号:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_one :subscription
end

订阅模式:

class Subscription < ActiveRecord::Base
  attr_accessible :status, :stripe_token, :user_id, :last_charge, :stripe_card_token
  belongs_to :user
  has_many :payments, :dependent => :destroy
  belongs_to :plan
  attr_accessor :stripe_card_token
end

我的 SubscriptionsController 新方法:

def new
    @subscription = User.find(current_user.id).build_subscription 
end

任何帮助表示赞赏,谢谢!

4

2 回答 2

13

我相信,当您传递 form_for 像 @subscription (具有订阅类)这样的对象时,默认情况下它需要 url subscriptions_path。但是由于您已将订阅声明为单一资源,因此将定义 url subscription_path。您应该明确指定此路线

<%= form_for @subscription, url: subscription_path, :html => { class: 'form_dark' } do |f| %>
于 2012-06-07T21:26:04.437 回答
0

两个政治可疑点,

1.您的应用中没有订阅控制器
2. 您是否subscriptions_path在应用的路由文件中定义了自定义路由?

阅读您的评论和更正后,我们只剩下路线问题

于 2012-06-07T21:15:32.583 回答