0

为什么 simpleform 不接受我的关联?

我对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
4

1 回答 1

0

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

于 2013-07-01T04:02:23.210 回答