2016-04-04 63 views
1

當我使用ajax調用創建/銷燬對象時,更新表(添加/刪除行)時出現問題。使用ajax調用更新部分多態對象的表Rails

我正在處理的應用程序可以將任務分配給各種類別,如項目,文檔等。任務模型通過與這些對象的多態關係實現。

我目前正在渲染任務列表和表單以創建一個新的任務作爲partials。 form_for新任務使用ajax來顯示/隱藏表單並創建一個新任務。

我遇到的問題是我能夠創建和銷燬任務,並且form_for似乎正在工作。但是,當我嘗試在js.erb調用中重新渲染局部時,我無法獲得表更新。該表只會在刷新時更新。

(我還在destroy操作上產生了一個406未知格式錯誤,我還沒有想出來,但由於銷燬和創建操作是相同的,我假設我仍然會有非更新問題)

任務控制器

class TasksController < ApplicationController 
    before_action :set_task, only: [:show, :edit, :update, :destroy] 
    before_action :logged_in_user, only: [:index, :my_tasks, :edit, :update, :destroy] 
    respond_to :html, :json, :js 
. 
. 
. 
    def create 
    @assignable = find_assignable 
    @task = @assignable.tasks.build(task_params) 
    if @task.save 
     respond_to do |format| 
      format.js 
      format.html {redirect_to @task} 
     end 
    else 
     respond_to do |format| 
      format.html { render :new } 
      format.json { render json: @task.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def new 
    @assignable = params[:assignable_type].constantize.find_by(params[:assignable_id]) 
    @task = Task.new 
    end 

    def destroy 
     @assignable = params[:assignable_type].constantize.find_by(params[:assignable_id]) 
     @assignable.tasks.find(params[:id]).destroy 
    respond_to do |format| 
     format.js 
     format.html {redirect_to :action => :index, status: 303 } 
     format.json { head :no_content } 
    end 
    end 
. 
. 
. 
def find_assignable 
    params.each do |name, value| 
     if name =~ /(.+)_id$/ 
      return $1.classify.constantize.find(value) 
     end 
    end 
     nil 
end 

在那裏我創建任務列表 任務的部分/ _task_list

%tbody 
     - assignable.tasks.each do |task| 
     -# the path builder below only works if there is one underscore in the object name. MasterPo --> master_pos 
     - path = "#{task.assignable_type.gsub(/([a-z])(?=[A-Z])/,'\1_\2').downcase.pluralize}/#{assignable.id}/tasks/#{task.id}" 
     %tr 
      %td= best_in_place task, :description, :url => path 
      %td= best_in_place task, :priority, :url => path 
      %td= best_in_place task, :status, :as => :select, 
      :collection => {"1" => "Approved", "2" => "Pending", "3" => "Outstanding", "4" => "Review"}, :url => path 
      %td= best_in_place task, :created_by, :url => path 
      %td= task.assignable_type 
      %td= link_to 'delete', 
      task_path(assignable, assignable_id: assignable.id, assignable_type: assignable.class.name, id: task.id), 
      remote: true, method: :delete, data: {confirm: 'Are you sure'} 

    %br 

    .row 
    .col-md-2.col-md-offset-4 
     = link_to new_task_path(assignable, assignable_id: assignable.id, assignable_type: assignable.class.name), remote: true do 
     %button.btn.btn-default New Task 

    .row 
    #task-form{:style => "display:none;"} 

    :javascript 
    $(document).ready(function() { 
    /* Activating Best In Place */ 
    jQuery(".best_in_place").best_in_place(); 
    }); 

下面給出參考其中要求我的窗體部分:

任務/ _form

= simple_form_for [@assignable, Task.new], remote: true do |f| 
    - if Task.new.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@task.errors.count, "error")} prohibited this task from being saved:" 
     %ul 
     - @task.errors.full_messages.each do |msg| 
      %li= msg 

    = f.input :description, as: :text 
    = f.input :priority, collection: 1..5 
    = f.input :status, collection: ["Open", "Completed", "Assigned"] 
    = f.button :submit 

現在,我假設我的錯誤必須在我試圖渲染方式我的_task_list部分在我的create.js.erb和我的destroy.js.erb中。該js.erb文件如下:

create.js.erb

$('#tasks').html("<%= j (render partial: 'tasks/task_list', locals: {assignable: @assignable}) %>"); 
$('#task-form').slideUp(350); 

new.js.erb

$('#task-form').html("<%= j (render 'form', locals: {assignable: @assignable}) %>"); 
$('#task-form').slideDown(350); 

destroy.js.erb

$('#tasks').html("<%= j (render partial: 'tasks/task_list', locals: {assignable: @assignable}) %>"); 

回答

0

問題是我在#tasks上調用ajax,而沒有真正給我的窗體div id ='tasks',所以沒有更新。不幸的是,我沒有包括頁面的頂部,所以在我的問題中不明顯

0

您的@assignable對象未重新載入。它仍包含以前的所有數據。在方法中再次定義它,以便它將被更新,並且表將在您的視圖中更新而不刷新。

+0

我試着在控制器和create.js.erb文件中定義@ assignable.reload ...當我調試它時顯示調用create.js視圖之前的新任務,但仍未更新視圖。我應該在哪裏重新定義它? – greatwhitenorth

+0

你問題解決了嗎? – Ajit