0
我正在關注在頁面上設置ajax的第4部分(服務器端關注點)。我完全複製了教程文本(用我自己的模型名稱替換),它創建並保存了我的「參與者」記錄並刷新了部分....但它在剛剛提交的表單下張貼了這個奇怪的代碼:在Ajax Rails表單提交( n n ...等)後,額外的Javascript代碼發佈
$("
\n\n Helper:[email protected] \n<\/li>\n\n").appendTo("#participants");
這似乎是我的部分信息和我的creat.js的一些奇怪的混搭。這不是一個真正的錯誤......只是額外的代碼。
這裏是我的代碼
class ParticipantsController < ApplicationController
def new
@participant = Participant.new
@participants = @participants.recently_updated
end
def create
@participant = Participant.new(participant_params)
respond_to do |format|
if @participant.save
format.html { redirect_to @participant, notice: 'Helper Invited!' }
format.js {}
format.json { render json: @participant, status: :created, location: @participant }
else
format.html { render action: "new" }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<ul id="participants">
<%= render @participants %>
</ul>
<%= form_for(@participant, remote: true) do |f| %>
<%= f.label :email %><br>
<%= f.email_field :email %>
<%= f.submit 'SUBMIT' %>
<script>
$(document).ready(function() {
return $("#new_participant").on("ajax:success", function(e, data, status, xhr) {
return $("#new_participant").append(xhr.responseText);
}).on("ajax:error", function(e, xhr, status, error) {
return $("#new_participant").append("<p>Oops. Please Try again.</p>");
});
});
</script>
<script>
$(function() {
return $("a[data-remote]").on("ajax:success", function(e, data, status, xhr) {
return alert("The helper has been removed and notified.");
});
});
</script>
_participant.html.erb
<li >
<%= participant.email %> <%= link_to participant, remote: true, method: :delete, data: { confirm: 'Are you sure?' } do %>REMOVE<% end %>
</li>
create.js.erb
$("<%= escape_javascript(render @participant) %>").appendTo("#participants");
destroy.js.erb
$('#participants').html("<%= j (render @participants) %>");