Active Record Associations 未定义方法 'val'(构建、创建由 has_many、belongs_to 启用)
Active Record Associations undefined method 'val' (build,create enabled by has_many,belongs_to)
我是 rails 的新手,在理解关联方面有些困难。我想做一个快速论坛(只是线程 - post 机制没有别的)。我的模型是通过以下方式生成的:
1. rails generate scaffold Forumthread title:string
2. rails generate scaffold Forumpost title:string content:text username:string
在我的模型中,我添加了关联:
class Forumthread < ActiveRecord::Base
has_many :forumposts, dependent: :destroy
end
class Forumpost < ActiveRecord::Base
belongs_to :forumthread
end
在主题的显示页面上,我希望能够为该主题创建一个论坛post。我正在尝试这样做:
看法:
<%=通知%>
<p>
<strong>Title:</strong>
<%= @forumthread.title %>
</p>
<% form_for(@post) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_forumthread_path(@forumthread) %> |
<%= link_to 'Back', forumthreads_path %>
和控制器:
def show
@current_thread = Forumthread.find_by_id(params[:id])
@post = @current_thread.forumposts.build
end
我没有进入创建部分,因为它似乎只输入 @current_thread.forumposts.build
来创建对象是行不通的。我错过了什么?
我希望 @post 成为 forumpost 类型的对象,这样我就可以使用 @current_thread.forumposts.create(forumposts_params)
;
创建
目前我收到以下错误:
undefined method `val' for #<Arel::Nodes::BindParam:0x007fe5c0648770>
如果需要,我很乐意提供更多数据! .
我记得读过有关缺少 foreign_key
(https://github.com/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a) 时发生的错误,看起来这可能是您的情况,因为您在为 Forumpost 生成脚手架时没有包含它。您的数据库 table 中是否有用于论坛帖子的 forumthread_id
列?如果你不知道我在说什么 - 转到 db/schema.rb 文件并检查你是否可以看到类似的内容:
create_table "forumsposts", force: true do |t|
#some other fields
t.integer "forumthread_id", null: false
#some other fields
end
如果没有,您将不得不生成 运行 另一个迁移,将缺少的 foreign_key
添加到 Forumspost。在 http://guides.rubyonrails.org/association_basics.html#options-for-belongs-to-foreign-key :), 运行 rails generate migration AddForumthreadIdToForumpost
上阅读相关内容,将类似下面的代码放入新创建的迁移文件中,然后 运行 rake db:migrate
:
class AddForumthreadIdToForumpost < ActiveRecord::Migration
def change
add_column :forumposts, :forumthread_id, :integer, null: false
add_index :forumposts, :forumthread_id
end
end
不确定您是否仍在处理此问题。但是,您是否在模型内部尝试过 accepts_nested_attributes_for
?
class Forumthread < ActiveRecord::Base
has_many :forumposts, dependent: :destroy
accepts_nested_attributes_for :forum_post
end
class Forumpost < ActiveRecord::Base
belongs_to :forumthread
accepts_nested_attributes_for :forum_thread
end
我是 rails 的新手,在理解关联方面有些困难。我想做一个快速论坛(只是线程 - post 机制没有别的)。我的模型是通过以下方式生成的:
1. rails generate scaffold Forumthread title:string
2. rails generate scaffold Forumpost title:string content:text username:string
在我的模型中,我添加了关联:
class Forumthread < ActiveRecord::Base
has_many :forumposts, dependent: :destroy
end
class Forumpost < ActiveRecord::Base
belongs_to :forumthread
end
在主题的显示页面上,我希望能够为该主题创建一个论坛post。我正在尝试这样做: 看法: <%=通知%>
<p>
<strong>Title:</strong>
<%= @forumthread.title %>
</p>
<% form_for(@post) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_forumthread_path(@forumthread) %> |
<%= link_to 'Back', forumthreads_path %>
和控制器:
def show
@current_thread = Forumthread.find_by_id(params[:id])
@post = @current_thread.forumposts.build
end
我没有进入创建部分,因为它似乎只输入 @current_thread.forumposts.build
来创建对象是行不通的。我错过了什么?
我希望 @post 成为 forumpost 类型的对象,这样我就可以使用 @current_thread.forumposts.create(forumposts_params)
;
目前我收到以下错误:
undefined method `val' for #<Arel::Nodes::BindParam:0x007fe5c0648770>
如果需要,我很乐意提供更多数据! .
我记得读过有关缺少 foreign_key
(https://github.com/rails/rails/commit/78bd18a90992e3da767cfe492f1bc5d63077da8a) 时发生的错误,看起来这可能是您的情况,因为您在为 Forumpost 生成脚手架时没有包含它。您的数据库 table 中是否有用于论坛帖子的 forumthread_id
列?如果你不知道我在说什么 - 转到 db/schema.rb 文件并检查你是否可以看到类似的内容:
create_table "forumsposts", force: true do |t|
#some other fields
t.integer "forumthread_id", null: false
#some other fields
end
如果没有,您将不得不生成 运行 另一个迁移,将缺少的 foreign_key
添加到 Forumspost。在 http://guides.rubyonrails.org/association_basics.html#options-for-belongs-to-foreign-key :), 运行 rails generate migration AddForumthreadIdToForumpost
上阅读相关内容,将类似下面的代码放入新创建的迁移文件中,然后 运行 rake db:migrate
:
class AddForumthreadIdToForumpost < ActiveRecord::Migration
def change
add_column :forumposts, :forumthread_id, :integer, null: false
add_index :forumposts, :forumthread_id
end
end
不确定您是否仍在处理此问题。但是,您是否在模型内部尝试过 accepts_nested_attributes_for
?
class Forumthread < ActiveRecord::Base
has_many :forumposts, dependent: :destroy
accepts_nested_attributes_for :forum_post
end
class Forumpost < ActiveRecord::Base
belongs_to :forumthread
accepts_nested_attributes_for :forum_thread
end