简体   繁体   中英

ajax form and partials

I have successfully added comments to public activity ie after i followed this tutorial on public activity #406 Public Activity and i will like the comments to be submitted by ajax, i have tried every tutorial out and does not work my codes so far includes

for the comments controller this is what its like

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

for the partial i created for each activity is like this eg

<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>

all i want to achieve is update this css id

<ul id="chat">
              <%= render activity.comments %>
            </ul> 

also my create.js.erb is something like this

$("#chat").append('<%= j render(activity.comments) %>');
$("#new_comment")[0].reset();

note, for some reasons this is how my partial is rendered out

<%= render activity.comments %> instead of the famous  `<%= render @activity.comments %>`

which the other is working properly, and also the whole code is displayed on the current_users showpage eg users/1 page

Several issues:

@comment.user = current_user

Where does @comment get initialized? It looks like you're calling a variable which doesn't exist, unless you've called it in a before_action callback?


Are you getting any errors? It's hard to go through masses of code without any guidance on what the issue may be. You need to post up some responses / logs from your app to show us how it's dealing with the requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM