notthehoff
notthehoff

Reputation: 1242

How to add rails form select from 1 to many related table?

Trying to include data from a related subscription_type table in a subscription table. What am I doing wrong here?

subscriptions_controller.rb

class SubscriptionsController < ApplicationController
    def new
        @subscription = Subscription.new
        @subscription_types = SubscriptionType.all
    end
end

models

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

the subscription 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 %>

When I go to /subscriptions/new, I get the following error:

undefined method `subscription_type_id' for #<Subscription:0x007fda59fe0b60>

and the following in the logs:

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)

Also here is the migration:

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

Upvotes: 0

Views: 31

Answers (1)

dgilperez
dgilperez

Reputation: 10796

My bet is that you are missing a migration to reflect your association. Add this migration, run rake db:migrate and restart.

class AddSubscriptionTypeIdToSubscriptions < ActiveRecord::Migration
  def change
    add_column :subscriptions, :subscription_type_id, :integer
  end
end

Upvotes: 1

Related Questions