Rails 不会将我的外键保存到数据库中
所以基本上我有两个模型:Entry 和 Comment。 我进行了关联设置,以便条目有许多评论:
class Entry < ActiveRecord::Base
has_many :comments
end
并且评论属于一个条目:
class Comment < ActiveRecord::Base
belongs_to :entry
end
在我的数据库模式中,我设置了评论表,其中包含名为entry_id的列。 据我所知,这就是我建立协会所需要做的全部工作。 但是,当我保存评论时,它不会将entry_id保存到数据库中。
我可以确认entry_id正在表单中传递。这是发送到控制器的 params 变量的转储。
{"comment"=>{"comment"=>"ghgghgh"},
"commit"=>"Create Comment",
"format"=>"",
"entry_id"=>"1",
"authenticity_token"=>"G4uH8smdA2eeKeTXbD9NbenKH4AbWLyJuPWQzRcn6CI=",
"utf8"=>"✓"}
有什么想法吗?
编辑: 这是我对内置评论表单的看法:
<% @entry.each do |e| %>
<div class="entry">
<p><%= e.entry %></p>
<small>Posted by <%= e.author %> at <%= e.created_at.strftime("%I:%M%p %m/%d/%Y") %></small>
<% if e.comments.nil? %>
<p>No Comments</p>
<% else %>
<% e.comments.each do |c| %>
<blockquote><%= c.comment %></blockquote>
<% end %>
<% end %>
<%= form_for @comment, :url => entry_comments_path(e, @comment) do |f| %>
<%= f.label :comment %>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>
</div>
<hr />
<% end %>
<%= button_to "Write A Message", new_entry_path, :method => :get %>
我有评论作为嵌套路由:
resources :entries do
resources :comments
end
So basically I have two models, Entry and Comment.
I have the association setup so that Entry has many Comments:
class Entry < ActiveRecord::Base
has_many :comments
end
And a comment belongs to an entry:
class Comment < ActiveRecord::Base
belongs_to :entry
end
In my database schema I have setup the comments table with a column called entry_id.
To my knowledge, this is all I needed to do to setup an association. However when I save a comment, it does not save the entry_id into the database.
I can confirm that the entry_id is being passed in the form. Here is a dump of the params variable being sent to the controller.
{"comment"=>{"comment"=>"ghgghgh"},
"commit"=>"Create Comment",
"format"=>"",
"entry_id"=>"1",
"authenticity_token"=>"G4uH8smdA2eeKeTXbD9NbenKH4AbWLyJuPWQzRcn6CI=",
"utf8"=>"✓"}
Any Ideas?
EDIT:
This is my view with the comment form built in:
<% @entry.each do |e| %>
<div class="entry">
<p><%= e.entry %></p>
<small>Posted by <%= e.author %> at <%= e.created_at.strftime("%I:%M%p %m/%d/%Y") %></small>
<% if e.comments.nil? %>
<p>No Comments</p>
<% else %>
<% e.comments.each do |c| %>
<blockquote><%= c.comment %></blockquote>
<% end %>
<% end %>
<%= form_for @comment, :url => entry_comments_path(e, @comment) do |f| %>
<%= f.label :comment %>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>
</div>
<hr />
<% end %>
<%= button_to "Write A Message", new_entry_path, :method => :get %>
I have commments as a nested route:
resources :entries do
resources :comments
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Entry_id 不在您的参数评论中。您需要:
在您的表单中,您的entry_id 需要位于评论部分。 comment[entry_id]
如果您想了解更多信息,我们需要您的意见。
您有两个选择
1) 在表单中添加entry_id
2) 在创建操作中添加它
您遇到此问题是因为您嵌套了表单。
The entry_id is not in your comment in your params. You need have :
In your form, you entry_id need to be in comment part. comment[entry_id]
If you want more information, we need your view.
You have two choice
1) add the entry_id in your form
2) add it in your create action
You have this problem because you nested your form.