简体   繁体   中英

Rails - jQuery/AJAX not refreshing partial

I've got a timeline view that shows partials for events - so you can CRUD events directly on the timeline view. What I'm now trying to do is to have it so that when you click 'delete' next to an event, the delete partial refreshes, so that the user can see that the event has been deleted. I can't seem to get this to work - the event does get deleted when the user clicks delete, but the partial doesn't refresh. You have to refresh the page to get the updated partial.

Also, please can someone tell me how I would request the updated contents of the "my-timeline" div, so that the deleted event doesn't appear in there too? Thanks!

timeline/show:

<div id="my-timeline"></div>
<div id="delete_events">
    <%= render :partial => "delete_events", :locals => { :events => current_user.events }%>     
</div>

timeline/_delete_events.html.erb:

<%= button_to 'Delete', event, confirm: "Are you sure?", method: :delete, :remote => true %>

events/delete.js.erb:

$('#delete_events').html('<%= escape_javascript( render :partial => "/timelines/delete_events", :locals => { :events => current_user.events } ) %>');

events controller:

def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to current_user.timeline, notice: 'Event was successfully removed.' }
      format.json { head :no_content }
      format.js
    end
  end

The javascript for generating the timeline (only located here temporarily!) into the my-timeline div:

<script>
            $(document).ready(function() {
                createStoryJS({
                    type:       'timeline',
                    width:      '820',
                    height:     '600',
                    source:     '/users/<%= current_user.id %>/events.json?callback',
                    embed_id:   'my-timeline',
                    font:       'Euphoria-Droid',
                    debug:      true,
                    gmap_key:   'foo',
                    maptype:    'HYBRID'
                });
            });
        </script>

i think you should change name from

  events/delete.js.erb

    to

 events/destroy.js.erb

and by write alert("ok") in destroy.js.erb check control comes here

Hope it works

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