Try this. has_many :focus, :class_name => "Focus", :foreign_key => 'judge_id'
. Its because the plural form of focus is 'foci'. Rails automatically figures out the class name and foreign key only when you give the proper plural form
Why isn't simpleform accepting my association?
-
02-06-2022 - |
Question
Why isn't simpleform accepting my association?
I'm relatively new to rails, so forgive me if this is a newb mistake. Because of that, I"m including potentially too much info, just to ensure that I'm not doing something stupid.
I'm attempting to build a simple form wherein a judge can select different focuses they want to judge.
I'm getting the following error message:
NameError in Devise/registrations#new
Showing /Users/wattsb/ruby/rails_projects/social_pitch/app/views/devise/registrations/new.html.erb where line #9 raised:
uninitialized constant Judge::Focu
Extracted source (around line #9):
6: <%= f.input :email %>
7: <%= f.input :password %>
8: <%= f.input :password_confirmation %>
9: <%= f.association :focus, as: :check_boxes %>
10: <div class="form-actions">
11: <%= f.submit "Sign up", class: "btn btn-primary" %>
12: </div>
The error references the following view
<h2>Sign up</h2>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.association :focus, as: :check_boxes %>
<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
The form references the following models:
class Judge < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :focus
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
end
class Focus < ActiveRecord::Base
attr_accessible :title
belongs_to :judge
end
For those models, I have done the following migrations:
class DeviseCreateJudges < ActiveRecord::Migration
def change
create_table(:judges) do |t|
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.datetime :remember_created_at
t.integer :sign_in_count, :default => 0
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
t.timestamps
end
add_index :judges, :email, :unique => true
add_index :judges, :reset_password_token, :unique => true
end
end
class AddNameToJudges < ActiveRecord::Migration
def change
add_column :judges, :name, :string
end
end
class CreateFocus < ActiveRecord::Migration
def change
create_table :focus do |t|
t.string :title
t.timestamps
end
end
end
class AddAssociationsBetweenJudgesAndFocuses < ActiveRecord::Migration
def up
change_table :focus do |t|
t.belongs_to :judge
end
end
end
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow