ajax形式和部分

时间:2014-03-01 06:18:17

标签: javascript ruby-on-rails ajax

我已经成功地为公共活动添加了评论,即在我关注公共活动#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">&mdash;  <%="#{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 showpage上,例如users / 1 page

1 个答案:

答案 0 :(得分:0)

几个问题:

@comment.user = current_user

@comment在哪里初始化?看起来你正在调用一个不存在的变量,除非你在before_action回调中调用它?


你有任何错误吗?如果没有任何关于问题的指导,很难通过大量的代码。您需要从您的应用中发布一些回复/日志,以向我们展示它如何处理请求