2012-12-05 70 views
0

enter image description here嵌套資源悲傷路徑呈現空白評論

我有一個評論資源嵌套在帖子資源。

def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.build(params[:comment]) 
    @comment.ip = request.remote_ip 
    if @comment.save 
     redirect_to post_path(@post, notice: "Comment was successfully created") 
    else 
     flash[:alert] = "Comment could not be created" 
     render 'posts/show' 
    end 
    end 

這一切工作不夠好,但我有一個嘮叨項目時與評論形式的職位/顯示頁面重新呈現,它表明,未通過驗證的內聯註釋。我想知道正確的方法來做到這一點,短視圖層中的一些邏輯,不顯示未保存的評論。

回答

0

我結束了在視圖中解決它,因爲我無法找到任何其他解決方案

<% @post.comments.each do |c| %> 
    <% unless c.new_record? %> 
    <strong><%= "#{c.name} wrote: " %></strong><br /> 
    <blockquote><%= c.body %></blockquote> 
    <% end %> 
<% end %>