尝试在订阅表中包含来自相关subscription_type表的数据。我在这里做错了什么?
subscriptions_controller.rb
class SubscriptionsController < ApplicationController
def new
@subscription = Subscription.new
@subscription_types = SubscriptionType.all
end
end
模型
subscription.rb
class Subscription < ActiveRecord::Base
belongs_to :subscription
belongs_to :subscription_type
end
subscription_type.rb
class SubscriptionType < ActiveRecord::Base
has_many :subscriptions
end
订阅new.html.erb
<h1>New subscription</h1>
<%= puts "$$$$$$$$$$$$$$$$$$$$$$$$$$$$"%>
<%= puts @subscription_types %>
<%= form_for @subscription do |f| %>
<% if @subscription.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @subscription.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :subscription_types, "Subscription Type" %><br />
<%= f.collection_select :subscription_type_id, @subscription_types, :id, :type_name, :prompt => "Select a prospect" %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
当我转到/ subscriptions / new时,我收到以下错误:
undefined method `subscription_type_id' for #<Subscription:0x007fda59fe0b60>
以及日志中的以下内容:
Started GET "/subscriptions/new" for 127.0.0.1 at 2015-02-28 17:06:46 -0600
Processing by SubscriptionsController#new as HTML
$$$$$$$$$$$$$$$$$$$$$$$$$$$$
SubscriptionType Load (0.2ms) SELECT "subscription_types".* FROM "subscription_types"
#<SubscriptionType:0x007fda5a1fab30>
#<SubscriptionType:0x007fda5a1fa770>
Rendered subscriptions/new.html.erb within layouts/application (2.3ms)
Completed 500 Internal Server Error in 4ms
ActionView::Template::Error (undefined method `subscription_type_id' for #<Subscription:0x007fda5a1e9ab0>):
15: <% end %>
16: <p>
17: <%= f.label :subscription_types, "Subscription Type" %><br />
18: <%= f.collection_select :subscription_type_id, @subscription_types, :id, :type_name, :prompt => "Select a prospect" %>
19: </p>
20: <p class="button"><%= f.submit %></p>
21: <% end %>
app/views/subscriptions/new.html.erb:18:in `block in _app_views_subscriptions_new_html_erb___1485229973545805917_70287896353840'
app/views/subscriptions/new.html.erb:5:in `_app_views_subscriptions_new_html_erb___1485229973545805917_70287896353840'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (33.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (40.9ms)
此处还有迁移:
class CreateSubscriptionTypes < ActiveRecord::Migration
def change
create_table :subscription_types do |t|
t.string :type_name
t.decimal :base_price, :precision =>4, :scale => 2
t.timestamps
end
end
end
答案 0 :(得分:1)
我敢打赌,您错过了迁移以反映您的关联。添加此迁移,运行rake db:migrate
并重新启动。
class AddSubscriptionTypeIdToSubscriptions < ActiveRecord::Migration
def change
add_column :subscriptions, :subscription_type_id, :integer
end
end