2009-11-20 33 views
0

好吧,我是新來的rails,所以我希望我解釋得很好。我似乎遇到了收集適當信息的表單問題。在一個地方而不是另一個地方工作。這是情況。Ajax問題 - Ruby on Rails - 錯誤:無法找到沒有ID的帖子

我創建了一個包含三張表,文章,評論和投票的博客。我的評論正常工作,部分投票工作。這是我的意思。

/posts/show.html.erb文件基本顯示帖子,相關的評論,並顯示帖子有多少票。我正在進行投票,但是使用一個帶有隱藏字段的表單,該表單的post_id值爲'1'。然後我使用AJAX返回@ post.votes.count。這一切都很好。

我想使用上面提到的同樣的投票/回報,但在/posts/index.html.erb頁面上。在索引頁面中,我有<%@ post.each do | post | %>做兩件事。首先渲染:partial => post%>,然後我在show.html.erb頁面上使用相同的代碼。我正在從所述終端收到錯誤是

的ActiveRecord :: RecordNotFound(沒有ID找不到郵政): 應用程序/控制器/ votes_controller.rb:4:在`創建」

我猜測這與它是索引記錄集的一部分有關,該索引記錄集沒有與該頁面關聯的唯一後期參數。我希望有一個簡單的修復。下面是代碼:

這是正常工作的/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 

我希望這是有道理的。讓我知道是否需要澄清任何事情。

+0

你可以發佈你的節目和索引從帖子的控制器操作? – 2009-11-20 06:30:07

回答

0

貌似我只需要更改<%@ posts.each下面就|文章| %>部分的index.html.erb文件。

<% remote_form_for [@post, Vote.new] do |f| %> 
     <%= f.hidden_field :vote, :value => '1' %> 
     <%= f.submit "Like" %> 

<% remote_form_for [post, Vote.new] do |f| %> 
     <%= f.hidden_field :vote, :value => '1' %> 
     <%= f.submit "Like" %> 
0

我想這是造成問題。你沒有張貼自己的帖子控制器,但確實動作設定@post指數:?

<%= render :partial => @post %> 
+0

這就是我的想法,但當我嘗試時會攪亂一切。基本上整個職位身體消失。 – bgadoci 2009-11-20 06:04:09