为什么不接受我的关联?

时间:2013-06-30 23:48:13

标签: ruby-on-rails views simple-form rails-models

为什么不接受我的关联?

我对rails很新,所以如果这是一个新的错误,请原谅我。因此,我可能会包含太多信息,只是为了确保我没有做一些愚蠢的事情。

我正在尝试建立一个简单的表格,其中法官可以选择他们想要判断的不同焦点。

我收到以下错误消息:

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>

该错误引用了以下视图

<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" %>

表单引用了以下模型:

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

对于这些模型,我完成了以下迁移:

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

1 个答案:

答案 0 :(得分:0)

试试这个。 has_many :focus, :class_name => "Focus", :foreign_key => 'judge_id'。因为复数形式的焦点是“焦点”。只有当您提供正确的复数形式时,Rails才会自动计算出类名和外键