为什么我的 Rails Show 操作在选择后会创建新记录?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,你不想使用
但是
As far as I know, you dont want to use
but