我是开发新手,并且花了最后12个小时(字面意思)试图弄清楚这个错误信息 - 我放弃了夜晚,但没有在快速呼救之前寻求帮助计算器。
我有这样的表格:
<h2>Select from the language options below (or, <%= button_to "Login", 'users/login', method: :get %></h2>
<%= form_for @language_role do |f| %>
<div id="input">
<h3>I want to learn:</h3><%= select_tag(:language_id, options_from_collection_for_select(Language.all, :id, :lang_name)) %>
</div>
<div>
<p><%= f.submit "Start learning" %></p>
</div>
<% end %>
这给了我这条错误消息,突出显示行@language_role = current_user.language_roles.build
:&#34;未定义的方法`language_roles&#39;为零:NilClass&#34;
我有三张桌子:
create_table "language_roles", force: :cascade do |t|
t.integer "language_id"
t.boolean "is_active"
t.boolean "is_teacher"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["user_id"], name: "index_language_roles_on_user_id"
end
create_table "languages", force: :cascade do |t|
t.string "lang_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
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.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
language_roles
表旨在允许用户拥有多种语言,以及该语言中的许多角色。以下是我的班级定义:
class LanguageRole < ApplicationRecord
belongs_to :languages
belongs_to :users
end
class Language < ApplicationRecord
has_many :language_roles
has_many :users, :through => :language_roles
end
class User < ApplicationRecord
has_many :language_roles
has_many :languages, :through => :language_roles
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我的根路径是#home#index&#39;,如果current_user.language_roles
为空,用户应选择语言。因此,我将此代码放在我的家庭控制器和language_roles控制器中:
class HomeController < ApplicationController
def index
@language_role = current_user.language_roles.build
end
end
class LanguageRolesController < ApplicationController
def create
@language_role = current_user.language_roles.build(language_role_params)
if @language_role.save
redirect_to root_path
else
redirect_to :back
end
end
private
def language_role_params
params.permit(:language_id)
end
end
问题到底是什么?我假设我需要以某种方式实例化变量,但我不确定如何。
谢谢, 迈克尔
答案 0 :(得分:1)
您的LanguageRole
模型中存在拼写错误:
LanguageRole < ApplicationRecord
belongs_to :languages
belongs_to :users
end
应该是
LanguageRole < ApplicationRecord
belongs_to :language
belongs_to :user
end
belongs_to
个关联必须使用单数术语。
声明has_many
关联时,其他模型的名称会复数化。
答案 1 :(得分:0)
您的current_user似乎没有定义。您可以安装一个名为'pry-rails'的gem,并在将来调整您的方式和其他任何方式。这是一个如何使用它的教程Railscasts #280
答案 2 :(得分:0)
在LanguageRole
模型中,您定义为belongs_to :users
。但它应该是belongs_to :user
。
你的模特看起来像......
LanguageRole < ApplicationRecord
belongs_to :languages
belongs_to :users
end
应该是
之类的东西LanguageRole < ApplicationRecord
belongs_to :language
belongs_to :user
end