Ajax 问题 - Ruby on Rails - 错误:无法找到没有 ID 的帖子
好吧,我是 Rails 新手,所以我希望我能很好地解释这一点。我似乎对收集正确信息的表格有疑问。在一个地方工作,而不是在另一个地方工作。情况是这样的。
我创建了一个包含三个表、帖子、评论和投票的博客。我的评论工作正常,投票部分工作。这就是我的意思。
/posts/show.html.erb 文件主要显示帖子、相关评论以及该帖子有多少票。我正在处理投票,但使用带有隐藏字段的表单,该表单将值“1”以及 post_id 传递到表中。然后我使用 AJAX 返回 @post.votes.count。这一切都很好。
我正在尝试使用上面提到的相同投票/返回,但在 /posts/index.html.erb 页面上。在索引页中我有 <% @post.each do |post| %>做两件事。第一次渲染:部分=>帖子 %>然后我有与 show.html.erb 页面上使用的相同的代码。我从终端收到的错误是
ActiveRecord::RecordNotFound (无法找到没有 ID 的帖子): app/controllers/votes_controller.rb:4:in `create'
我猜这与它是索引记录集的一部分有关,索引记录集没有与页面关联的唯一帖子参数。我希望有一个简单的修复方法。这是代码:
这是正常工作的 /posts/show.html.erb 代码:
<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %>
</div>
<%= render :partial => @post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= @post.votes.count %> People liked this BattleCry.
</div>
</div>
<div id="votebutton">
<% remote_form_for [@post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<div id="beltcommentsbottom">
</div><br/><br/>
<div id="belt">
<div id="belttext">
<p5>Add a Comment</p5>
<% remote_form_for [@post, Comment.new] do |f| %>
<p>
<%= f.text_area ( :body, :class => "commentarea") %>
</p>
<%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<div id="comments">
<%= render :partial => @post.comments %>
</div>
<br/>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
这是它在索引页中的位置:
<% @posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => @post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= post.votes.count %> People liked this BattleCry.
</div>
<%= link_to "Comments (#{post.comments.count})", post %>
</div>
<div id="votebutton">
<% remote_form_for [@post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<br/>
<br/>
<br/>
<% end %>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
这是 votes_controller.rb
class VotesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@vote = @post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
这是 /votes/create.js
page.replace_html :votes, :partial => @vote
page[@vote].visual_effect :highlight
最后,这里是部分,/_vote.html.erb:
page.replace_html :votes, :partial => @vote
page[@vote].visual_effect :highlight
我希望这是有道理的。如果我需要澄清任何事情,请告诉我。
Ok, I am new to rails so I hope I explain this well. I seem to have a problem with a form collecting the proper information. Work in one spot and not another. Here is the situation.
I have created a blog with three tables, posts, comments, and votes. I have comments working correctly and votes working partially. Here is what I mean.
The /posts/show.html.erb file basically shows the post, the associated comments and it shows how many votes that post has. I am working the votes but using a form with a hidden field that delivers a value of '1' along with the post_id to the table. I am then using AJAX to return the @post.votes.count. This all works great.
I am trying to use this same vote/return mentioned above but on the /posts/index.html.erb page. In the index page I have the <% @post.each do |post| %> doing two things. First render :partial => post %> and then I have the same code that I am using on the show.html.erb page. The error that I am getting from the terminal is
ActiveRecord::RecordNotFound (Couldn't find Post without an ID):
app/controllers/votes_controller.rb:4:in `create'
I am guessing this has to do with it being part of the index record set which doesn't have a unique post param associated with the page. I am hoping there is a simple fix. Here is the code:
This is the /posts/show.html.erb code that is working correctly:
<div id="backto"<%= link_to 'Back to all BattleCries', posts_path %>
</div>
<%= render :partial => @post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= @post.votes.count %> People liked this BattleCry.
</div>
</div>
<div id="votebutton">
<% remote_form_for [@post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<div id="beltcommentsbottom">
</div><br/><br/>
<div id="belt">
<div id="belttext">
<p5>Add a Comment</p5>
<% remote_form_for [@post, Comment.new] do |f| %>
<p>
<%= f.text_area ( :body, :class => "commentarea") %>
</p>
<%= f.submit "Add Comment"%>
<% end %>
</div>
<div id="beltbottom">
</div>
</div><br/>
<div id="comments">
<%= render :partial => @post.comments %>
</div>
<br/>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is where it is in the index page:
<% @posts.each do |post| %>
<%= render :partial => post %>
<%= render :partial => @post %>
<div id="beltcomments">
<div id="beltcommenttext">
<div id="vote">
<div id="votes">
<%= post.votes.count %> People liked this BattleCry.
</div>
<%= link_to "Comments (#{post.comments.count})", post %>
</div>
<div id="votebutton">
<% remote_form_for [@post, Vote.new] do |f| %>
<%= f.hidden_field :vote, :value => '1' %>
<%= f.submit "Like" %>
<% end %>
</div>
</div>
</div>
<br/>
<br/>
<br/>
<% end %>
<div id="commenttime">
Copyright 2009, My Life BattleCry, LLC.
</div>
<br/>
<br/>
<br/>
Here is the votes_controller.rb
class VotesController < ApplicationController
def create
@post = Post.find(params[:post_id])
@vote = @post.votes.create!(params[:vote])
respond_to do |format|
format.html { redirect_to @post}
format.js
end
end
end
Here is the /votes/create.js
page.replace_html :votes, :partial => @vote
page[@vote].visual_effect :highlight
Lastly, here is the partial, /_vote.html.erb:
page.replace_html :votes, :partial => @vote
page[@vote].visual_effect :highlight
I hope this makes sense. Let me know if I need to clarify anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这会引起问题。您没有发布帖子控制器,但索引操作是否设置了
@post
?:I imagine this is causing problems. You didn't post your posts controller, but does the index action set
@post
?:看起来我只需要在 <% @posts.each do |post| 中更改以下内容%> index.html.erb 文件的部分。
到
Looks like I just needed to change the following in the <% @posts.each do |post| %> section of the index.html.erb file.
to