gpt4 book ai didi

ruby-on-rails - Rails 参数错误 "Unknown Key"?

转载 作者:数据小太阳 更新时间:2023-10-29 08:49:14 24 4
gpt4 key购买 nike

所以我有一个 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

最佳答案

问题是您在这一行中使用了裸词 order,而不是 :order:

has_many :submissions, order => ('updated_at DESC')

order 是一种返回 ActiveRecord::Relation 的方法,想想 Folder.order(:id)...。您正在尝试将该返回的对象用作散列中的键。这是您看到的错误。

关于ruby-on-rails - Rails 参数错误 "Unknown Key"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17580290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com