我已经成功地向公共活动添加了评论,即在我遵循了公共活动#406 Public Activity的这篇教程之后,我希望ajax提交评论,我已经尝试了每一个教程,到目前为止我的代码还不起作用,包括
对于注释控制器,它就是这样的
def create
@activity = Activity.find(params[:activity_id])
@comment = @activity.comments.create!(comment_params)
@comment.user = current_user
@users= User.joins(:comments).where(talks: {id: @activity.talk_ids}).push(@comment.user).reject {|user| user == @comment.user }.uniq
@users.each do |user|
@comment.create_activity :create, owner: user
end
respond_to do |format|
format.html { redirect_to user_path(current_user), notice: 'Comment created.' }
format.js
end
end
end对于我为每个活动创建的部分,如下所示
<div class="media social-box">
<a class="pull-left social-users-avatars" href="#">
<%= link_to image_tag(activity.user.image.url(:small)) ,activity.user%>
<p><%= link_to activity.user.username, activity.user if feed.activity %>
<span style="font-size: 11px; color:#4edee1;">Added this item </span> </p>
</a>
<ul class="unstyled custumer_say">
<li class="clearfix" style="list-style: none">
<%= link_to image_tag(sell.image.url,:style=> "width: 40%; padding-right: 10px;", :class=> "pull-left img_client"), sell%>
<div class="entry-content">
<header>
<span class="entry-date">— <%="#{time_ago_in_words(sell.created_at)} ago "%> </span>
</header>
</div>
</li>
</ul>
<div class="media-body social-body">
<div class="social-footer">
<div class="social-info-users">
<strong><%= pluralize(activity.comments.size, "Comment") %></strong>
</div>
<div class="social-comments">
<ul id="chat">
<%= render activity.comments %>
</ul>
<% if current_user.friends.present? %>
<div class="media">
<a class="pull-left" href="#">
<%= image_tag(current_user.image.url(:tiny),:style=> "width: 100%;")%>
</a>
<div class="media-body">
<%= form_for([activity, activity.comments.build],:remote => true) do |f| %>
<%= f.text_field :details, :class=>"input-block-level", :placeholder=>"write a comment" %>
<% end%>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>我想要实现的就是更新这个css id
<ul id="chat">
<%= render activity.comments %>
</ul> 另外,我的create.js.erb是这样的
$("#chat").append('<%= j render(activity.comments) %>');
$("#new_comment")[0].reset();请注意,由于某些原因,这就是我的部分内容的呈现方式
<%= render activity.comments %> instead of the famous `<%= render @activity.comments %>`另一个正常工作,并且整个代码也显示在current_users展示页面上,例如用户/1个页面
发布于 2014-03-01 19:08:42
几个问题:
@comment.user = current_user@comment是在哪里初始化的?看起来你在调用一个不存在的变量,除非你在before_action回调中调用了它?
您是否收到任何错误?如果没有任何关于问题可能是什么的指导,就很难浏览大量代码。您需要从您的应用程序发布一些响应/日志,以显示它是如何处理请求的
https://stackoverflow.com/questions/22110925
复制相似问题