删除成功后刷新表数据

时间:2023-01-18 07:57:47

looking at how i can refresh the event list partial after a successful destroy has gone through

看看我如何在成功销毁之后刷新事件列表部分

_event_list.haml (partial for refresh)

_event_list.haml(部分用于刷新)

%table.twelve#events
  %thead
    %tr
      -#%th Game/Event
      -#%th Result
      -#%th Team
      %th Opponent
      -#%th Date
      -#%th Time
      %th Location
      - if can? :update, @event
        %th.span1 Manager
  %tbody
    - @events.each do |event|
      %tr
        -#%td= event_display(event.event)
        -#%td= find_result(event)
        -#%td= event.team.name
        %td= link_to event.opponent.name, event
        -#%td= check_date(event)
        -#%td
          = "#{event.datetime.to_s(:event_time)} - #{end_time(event)}"
          %div{:style => 'font-size: .8em;color:#666; margin-top: 3px'}= "Arrived at #{arrive_time(event)}"
        %td
          - if event.location
            = link_to event.location.name, event, :rel => 'tooltip-html', :title => 'View ' + event.location.name
        - if can? :update, @event
          %td
            .btn-group
              %button.btn.dropdown-toggle{"data-toggle" => "dropdown"}
                %i.icon-pencil
                Manage
                %span.caret
              %ul.dropdown-menu
                %li= link_to "Edit #{event_display(event.event)}", edit_event_path(event)
                %li= link_to "Delete #{event_display(event.event)}", event, :method => :delete, :remote => :true, :class => 'deleteRecord'

destroy.js.erb $("events").html("<%= escape_javascript(render(:partial => "events_list")) %>");

destroy.js.erb $(“events”)。html(“<%= escape_javascript(render(:partial =>”events_list“))%>”);

events_controller

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

    respond_to do |format|
      format.html { redirect_to(events_url,
                                :notice => "#{event_display_c(@event.event)} vs #{@event.opponent.name} was successfully deleted.") }
      format.js
    end
  end

1 个解决方案

#1


3  

you should declare @events on the destroy action

你应该在destroy动作上声明@events

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

  respond_to do |format|
    format.html { redirect_to(events_url, notice: "#{event_display_c(@event.event)} vs #{@event.opponent.name} was successfully deleted.") }
    format.js { @events = Event.all # or something }
  end
end

then change destroy.js.erb to (assuming jquery)

然后将destroy.js.erb更改为(假设jquery)

$("#events").replaceWith("<%= escape_javascript render('events_list') %>");

#1


3  

you should declare @events on the destroy action

你应该在destroy动作上声明@events

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

  respond_to do |format|
    format.html { redirect_to(events_url, notice: "#{event_display_c(@event.event)} vs #{@event.opponent.name} was successfully deleted.") }
    format.js { @events = Event.all # or something }
  end
end

then change destroy.js.erb to (assuming jquery)

然后将destroy.js.erb更改为(假设jquery)

$("#events").replaceWith("<%= escape_javascript render('events_list') %>");