删除成功后刷新表数据

时间:2013-02-05 13:39:42

标签: ruby-on-rails jquery destroy

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

_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“))%>”);

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 个答案:

答案 0 :(得分:3)

你应该在销毁行动

上声明@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

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

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