この jQuery コードは 2 つのアクションを有効にする必要があります。
1 番目のアクションは、7 秒ごとにページを自動リロードすることです(refreshPartial();) 2 番目のアクションは、リンクがクリックされたときのフィールド
への自動入力です。chat_box
2 番目のアクションは、「refreshPartial();」を削除した場合にのみ正常に機能します。下から。
コンフリクトエラーか何かですか?どうすれば修正できますか?
<% if current_user %>
<% content_for(:head) do %>
<%= javascript_tag do %>
jQuery(document).ready(function () {
refreshPartial();
$('a#username').click(function() {
$(".chat_box#body_input").val($(this).attr('value'));
});
});
function refreshPartial() {
$.ajax({
url: "<%= show_user_path(@user) %>/refresh_part?page=<%= params[:page] %>",
type: "GET",
dataType: "script",
});
}
<% end %>
<% end %>
<% end %>
refresh_part.js.erb
$('#chat_comment').html("<%= j(render(:partial => 'users/comment')) %>");
setTimeout(refreshPartial,7000);
コントローラ
def refresh_part
@comments = @user.comment_threads.order("updated_at DESC").page(params[:page]).per(10)
@comment = @user.comment_threads.build
respond_to do |format|
format.js
end
end
View(フォーム入力欄)
<a name="comment_part"></a>
<form accept-charset="UTF-8" action="/users/John_Smith/comments" class="new_comment" data-remote="true" enctype="multipart/form-data" id="new_comment" method="post">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="vvwHabqywaDXv0Sv5NrbPP5kfdwhfewovbHkegkOUm2/2uJdNs=" />
<input class="chat_box" id="body_input" name="comment[body]" type="text" />
<button type="submit" class="btn">Submit</button>
</form>