为什么我的 Rails Show 操作在选择后会创建新记录?

发布于 2024-12-10 14:48:46 字数 2200 浏览 0 评论 0原文

Rails nooby,所以我不知道要提供什么信息。基本上,当我的显示操作从图片控制器调用时,就会插入新的评论记录。我的输出如下所示:

Started GET "/pictures/2" for 127.0.0.1 at Wed Oct 19 18:43:24 -0400 2011
  Processing by PicturesController#show as HTML
  Parameters: {"id"=>"2"}
  Picture Load (0.2ms)  SELECT "pictures".* FROM "pictures" WHERE "pictures"."id" = $1     LIMIT 1  [["id", "2"]]
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."picture_id"     = 2
   (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "comments" ("comment", "created_at", "downvote", "picture_id",     "updated_at", "upvote") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["comment", nil],     ["created_at", Wed, 19 Oct 2011 22:43:24 UTC +00:00], ["downvote", nil], ["picture_id", 2],     ["updated_at", Wed, 19 Oct 2011 22:43:24 UTC +00:00], ["upvote", nil]]
   (1.1ms)  COMMIT
Rendered pictures/show.html.erb within layouts/application (32.2ms)
Completed 200 OK in 51ms (Views: 41.0ms | ActiveRecord: 7.5ms)

显示控制器:

def show
  @picture = Picture.find(params[:id])
  @comments = @picture.comments
  respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @picture }
  end
end

显示视图:

<p id="notice"><%= notice %></p>

<h2><%= @picture.title %></h2> <br />
    <ul class="media-grid">
     <li>
     <a href="#">
     <%= image_tag @picture.url %>
    </a>
    </li>
  </ul>
<table>
<% @comments.each do |comment| %>
  <tr>
    <td><%= comment.comment %></td>
<td><%= comment.upvote %></td>
<td><%= comment.downvote %></td>
</tr>
<% end %>
</table>
<%= form_for [@picture, @picture.comments.create] do |f| %>
  <%= f.label "Comment:" %><br />
  <%= f.text_field :comment %>
  <%= f.submit %>
<% end %>

<%= link_to 'Back', pictures_path %>

模型:

class Comment < ActiveRecord::Base
  belongs_to :picture
end

class Picture < ActiveRecord::Base
  has_many :comments
end

Rails nooby so I don't know what info to provide. Basically when my show action gets called from the pictures controller a new comment record is inserted. My output looks like this:

Started GET "/pictures/2" for 127.0.0.1 at Wed Oct 19 18:43:24 -0400 2011
  Processing by PicturesController#show as HTML
  Parameters: {"id"=>"2"}
  Picture Load (0.2ms)  SELECT "pictures".* FROM "pictures" WHERE "pictures"."id" = $1     LIMIT 1  [["id", "2"]]
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."picture_id"     = 2
   (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO "comments" ("comment", "created_at", "downvote", "picture_id",     "updated_at", "upvote") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["comment", nil],     ["created_at", Wed, 19 Oct 2011 22:43:24 UTC +00:00], ["downvote", nil], ["picture_id", 2],     ["updated_at", Wed, 19 Oct 2011 22:43:24 UTC +00:00], ["upvote", nil]]
   (1.1ms)  COMMIT
Rendered pictures/show.html.erb within layouts/application (32.2ms)
Completed 200 OK in 51ms (Views: 41.0ms | ActiveRecord: 7.5ms)

Show Controller:

def show
  @picture = Picture.find(params[:id])
  @comments = @picture.comments
  respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @picture }
  end
end

Show View:

<p id="notice"><%= notice %></p>

<h2><%= @picture.title %></h2> <br />
    <ul class="media-grid">
     <li>
     <a href="#">
     <%= image_tag @picture.url %>
    </a>
    </li>
  </ul>
<table>
<% @comments.each do |comment| %>
  <tr>
    <td><%= comment.comment %></td>
<td><%= comment.upvote %></td>
<td><%= comment.downvote %></td>
</tr>
<% end %>
</table>
<%= form_for [@picture, @picture.comments.create] do |f| %>
  <%= f.label "Comment:" %><br />
  <%= f.text_field :comment %>
  <%= f.submit %>
<% end %>

<%= link_to 'Back', pictures_path %>

Models:

class Comment < ActiveRecord::Base
  belongs_to :picture
end

class Picture < ActiveRecord::Base
  has_many :comments
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半寸时光 2024-12-17 14:48:46

据我所知,你不想使用

<%= form_for [@picture, @picture.comments.create] do |f| %>

但是

<%= form_for [@picture, @picture.comments.new] do |f| %>

As far as I know, you dont want to use

<%= form_for [@picture, @picture.comments.create] do |f| %>

but

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