Pour mon application, j'ai des projets où les utilisateurs peuvent faire des commentaires (class Newcomments). En ce moment, il affiche très bien, mais sur la page d'actualisation. J'essaye d'employer AJAX/JQUERY de sorte qu'il publiera sans actualisation de page. Je suis ce tutoriel railscasts.Rails - AJAX/JQuery ne fonctionne pas
Donc maintenant, les publications sont faites à ma base de données et la page ne se réactualise pas. Mais quand je me rafraîchis, les messages apparaissent. La page que je rends mes commentaires pour un projet spécifique est sur les projets/_comments.html.erb.
Question: Comment est-ce que je l'ajusterais pour que le nouveau commentaire ait rendu?
newcomments_controller.rb
def create
@newcomment = @commentable.newcomments.new(params[:newcomment])
if @newcomment.save
respond_to do |format|
format.html { redirect_to comments_project_path(@commentable) }
format.js
end
else
render :new
end
end
vue/newcomments/_form.html.erb
<span class="comment">
<%= form_for [@commentable, @newcomment], remote: true do |f| %>
<%= f.text_area :content, rows: 3, :class => "span8" %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.submit "Add Comment", :class => "btn btn-header" %>
<% end %>
</span>
vue/newcomments/create.js.erb
$('#newcomment').append('<%= j render(@newcomments) %>');
projects_controller.rb
def comments
@commentable = @project
@newcomments = @commentable.newcomments.newest.page(params[:comments_page]).per_page(10)
@newcomment = Newcomment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @project.comments }
end
end
projets/comments.html.erb
<%= render 'comments' %>
projets/_comments.html.erb
<%= render @newcomments %>
vue/newcomments/_newcomment.html.erb
<div class="comments">
<%= link_to newcomment.user.name %></strong>
Posted <%= time_ago_in_words(newcomment.created_at) %> ago
<%= newcomment.content %>
</div>
<span class="comment">
<%= form_for [@commentable, @newcomment] do |f| %>
<div class="field">
<%= f.text_area :content, rows: 3, :class => "span8" %>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.submit "Add Comment", :class => "btn btn-header" %>
</div>
<% end %>
<% unless newcomment.newcomments.empty? %>
<%= render @newcomments %>
<% end %>
</span>