1
評價模型評論
belongs_to :post
validates_presence_of :username
validates_presence_of :body
Post模型
has_many :comments
posts_controller
def show
@post = Post.find(params[:id])
@comments = @post.comments
@comment = Comment.new
end
comments_controler
def create
@comment = Comment.new(params[:comment])
if @comment.save
@comments = @comment.post.comments
respond_to do |format|
format.js
end
else
#what to do in here
end
end
個
create.js
$("#comments").html("<%= escape_javascript(render :partial => "comments", :collections => @comments) %>");
帖子show.html
<div id="comments">
<%= render :partial => "comments", :collection => @comments %>
</div>
<div id="comment-form">
<%= form_for(@comment, :method => :put, :remote => true) do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<% @comment.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</div>
<% end %>
<%= f.text_field :username %>
<%= f.text_area :body %>
<%= f.hidden_field :post_id, :value => @post.id %>
<%= f.submit %>
<% end %>
</div>
我想在評論寫的東西是在創建行動comments_controller,這樣我可以無需重新加載頁面顯示的驗證錯誤。任何幫助將不勝感激。如果有另一種解決方案來進行基於Ajax的評論,我還可以更改所有代碼。
出話題的評論:我會實現http://disqus.com/基於Ajax的意見。 – apneadiving 2011-01-24 18:25:27