所以我有一个 Rails 应用程序,用户可以在其中创建“提交”。现在,我正在尝试添加创建用于组织提交的文件夹的功能。但是,我似乎遇到了让文件夹模型工作的问题。我收到以下错误:
Unknown key: #<ActiveRecord::Relation:0x007f17cc75d498>
该错误表明它在此代码的第 21 行:
18: </div>
19:
20: <div id="submission-list-container">
21: <% current_user.folders.each do |i| %>
22: <a href='#'>
23: <div id="post-container">
24: <%= i.title %> <p id="created-time">Created <%= i.created_at.strftime("%e/%-m") %></p>
我使用创建了文件夹模型rails g model folder title:string
,我的模型如下所示:
class Folder < ActiveRecord::Base
attr_accessible :title, :user_id
belongs_to :user
has_many :submissions, order => ('updated_at DESC')
end
我的猜测是我可能错误地设置了用户、提交和文件夹之间的关系。这是我的用户和提交模型:
提交.rb:
class Submission < ActiveRecord::Base
belongs_to :folder
belongs_to :user
attr_accessible :content, :title, :user_id
end
用户.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :user_id, :submissions, :folders
# attr_accessible :title, :body
has_many :submissions
has_many :folders, through: :submissions
end
我的迁移目录也如下所示:
20130523233304_create_submissions.rb
20130530064506_devise_create_users.rb
20130621002458_add_user_id_to_submissions.rb
20130709213421_add_user_id_to_folders.rb
20130710042650_add_folder_id_to_submissions.rb
20130710200424_create_folders.rb
知道有什么问题吗?这是我第一次遇到这个错误,所以我不确定我捏造了什么。
编辑 这里是文件夹和提交的 Schema.rb 表:
create_table "folders", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "submissions", :force => true do |t|
t.string "title"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end
编辑 2 我的 create_folders 迁移:
class CreateFolders < ActiveRecord::Migration
def change
create_table :folders do |t|
t.string :title
t.timestamps
end
结束结束
这是我的 add_folder_id_to_submissions 迁移文件:
class AddFolderIdToSubmissions < ActiveRecord::Migration
def change
end
end