我想開發一個ajax功能來評論我網站上的帖子。Rails嘗試執行更新操作而不是我想要的操作
我以前做過這個,但我不知道爲什麼我這次有問題。 Rails從posts_controller執行Update動作,而不是稱爲「save_comment」的動作。
這是我的路線文件的相關行:
map.connect "/posts/save_comment", :controller => 'posts', :action => 'save_comment'
這是視圖代碼:
<%= javascript_include_tag "prototype" %>
<% if logged_in? %>
<% remote_form_for :post, PostComment.new, :url => {:action => 'save_comment',:post_id=>inside_list.id}, :html => { :method => :put} do |f| %>
<p>
<%= f.label 'Comment' %><br />
<%= f.text_area :comment, :style=>'height:100px;' %>
</p>
<p>
<%= f.submit 'Publish' %>
</p>
<% end %>
<% end %>
的save_comment動作看起來是這樣的:
def save_comment
comment = PostComment.new
comment.user_id = current_user.id
comment.post_id = params[:post_id]
comment.comment = params[:post][:comment]
comment.save
post = Post.find(params[:post_id])
render :update do |page|
page.replace_html 'dComments', :partial => 'post_comments/inside_list', :object => post
end
end
BTW:有沒有更好的方法來做到這一點?
什麼的'save_comment'動作是什麼樣子? – 2010-08-17 19:22:06
我已經編輯了該信息的帖子 – 2010-08-17 19:24:14