Rails 不会将我的外键保存到数据库中

发布于 2024-09-27 06:22:26 字数 1548 浏览 1 评论 0原文

所以基本上我有两个模型: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-10-04 06:22:26

Entry_id 不在您的参数评论中。您需要:

 {"comment"=>{"comment"=>"ghgghgh", "entry_id"=>"1"},
"commit"=>"Create Comment",
"format"=>"",
"authenticity_token"=>"G4uH8smdA2eeKeTXbD9NbenKH4AbWLyJuPWQzRcn6CI=",
"utf8"=>"✓"}

在您的表单中,您的entry_id 需要位于评论部分。 comment[entry_id]

如果您想了解更多信息,我们需要您的意见。

您有两个选择

1) 在表单中添加entry_id

<%= form_for @comment, :url => entry_comments_path(e, @comment) do |f| %>
<%= f.label :comment %>
<%= f.text_area :comment %>
<%= f.hidden_field :entry_id, e.id
<%= f.submit %>

2) 在创建操作中添加它

def create
  Comment.create(params[:comment].merge(:entry_id => params[:entry_id])
end

您遇到此问题是因为您嵌套了表单。

The entry_id is not in your comment in your params. You need have :

 {"comment"=>{"comment"=>"ghgghgh", "entry_id"=>"1"},
"commit"=>"Create Comment",
"format"=>"",
"authenticity_token"=>"G4uH8smdA2eeKeTXbD9NbenKH4AbWLyJuPWQzRcn6CI=",
"utf8"=>"✓"}

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

<%= form_for @comment, :url => entry_comments_path(e, @comment) do |f| %>
<%= f.label :comment %>
<%= f.text_area :comment %>
<%= f.hidden_field :entry_id, e.id
<%= f.submit %>

2) add it in your create action

def create
  Comment.create(params[:comment].merge(:entry_id => params[:entry_id])
end

You have this problem because you nested your form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文