2013-02-26 35 views
0

現在我正在使用AJAX創建評論。但是,當我點擊輸入文本框仍然填充。我希望字段刷新(並且仍然可以在另一條評論中寫入)。沒有錯誤,但它仍然不會刷新文本框。創建部分工作正常。創建w/o刷新後清除text_field

create.js/ERB:(需要在這裏固定第二線,使得其完全替換)

$('#<%= dom_id(@micropost) %>').html("<%= escape_javascript(render(:partial => @micropost.comments))%>") 
$("#comment_field_<%[email protected]%>").replaceWith("<%= escape_javascript(render 'shared/comment_form', micropost: @micropost) %>") 

微柱/ _micropost:

<span id="<%= dom_id(micropost) %>"><%= render micropost.comments %></span> 
<span id="comment_field_<%=micropost.id%>"><%= render 'shared/comment_form', micropost: micropost if signed_in? %></span> 

共享/ Comment_form:

<%= form_for @comment, id:"comment_form", remote: true do |f| %> 
    <%= hidden_field_tag :micropost_id, micropost.id %> 
    <div id="comment_field"> 
    <%= link_to gravatar_for((current_user), size: 29) %> 
    <%= f.text_field :content, placeholder: "Say Something...", id: "comment_text_field", :style => "width: 508px; text-indent: 5px" %> 
    </div> 
<% end %> 
+0

請後的共享/ comment_form'的控制器動作和內容太。 – 2013-02-27 04:41:48

+0

我原本是這麼寫的,因爲我無法接受我的答案,而且我也不想浪費人們的時間 – Jaqx 2013-04-12 20:39:20

回答

0

需要僅使用jquery通過設置空值來清除text_field

create.js.erb

$("#comment_text_field_<%[email protected]%>").val("") 

最佳實踐使用獨特的動態ID,但可能會工作無論哪種方式。

共享/ Comment_form:

<%= f.text_field :content, id: "comment_text_field_#{micropost.id}" %>