Ruby on rails 基于Ajax的micropost';s评论(RubyonRails)
我在用户页面上有基于ajax的micropost的评论。它可以工作,但不正确。当我向anymicropost提交新评论时,它总是在lastmicropost上发布。然后,如果我用“F5”刷新页面,一切都会就绪——新的注释位于正确的微柱中。提前谢谢 comment.rbRuby on rails 基于Ajax的micropost';s评论(RubyonRails),ruby-on-rails,ruby,ajax,comments,Ruby On Rails,Ruby,Ajax,Comments,我在用户页面上有基于ajax的micropost的评论。它可以工作,但不正确。当我向anymicropost提交新评论时,它总是在lastmicropost上发布。然后,如果我用“F5”刷新页面,一切都会就绪——新的注释位于正确的微柱中。提前谢谢 comment.rb class Comment < ActiveRecord::Base attr_accessible :comment_content belongs_to :user belongs_to :micropost
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
class注释
评论\u controller.rb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
class CommentsController
\u micropost.html.erb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
五年前发的。
\u comment\u form.html.erb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
true do | f |%>
“40x2”%>
评论
\u comment.html.erb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
前一个月发的。
create.js.erb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end
class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<div id="comments">
<%= render micropost.comments %>
</div>
</td>
</tr>
<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_area :comment_content, :size => "40x2" %>
<button class="btn" type="submit">
Comment
</button>
<% end %>
<span style="width: 100%; background:#dff0d8"><%= wrap(comment.comment_content) %></span>
<span class="timestamp">
Posted by <%= comment.user.name %> <%= time_ago_in_words(comment.created_at) %> ago.
</span>
$('#comments').html("<%= escape_javascript(render(:partial => @micropost.comments)) %>");
$('#comments').html(“@micropost.comments))%>”;
也许您可以更改
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
到
也许你可以改变
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
到
我不明白你的问题。你有错误吗?没有错误,但是评论只发布到最后一个微页面。如果你的视图调用你的JS怎么办?对不起,我找到了,我在application.html.erb中用in_stylesheets.html.erb调用JS。我有:我不明白你的问题。你有什么错误吗?没有错误,但是评论只发布到最后一个微页面。如果你的视图调用你的JS怎么办?对不起,我找到了,我在application.html.erb中用in_stylesheets.html.erb调用JS。我有:谢谢,但也有同样的问题。谢谢,但也有同样的问题。