1

我試圖將Ajax與我的CRUD一起使用。我正在關注這個tutorialRails 3.1中的'Template is missing'錯誤在嘗試渲染時

我看到這個錯誤:

Missing template posts/edit, application/edit with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * "/Users/wangstabill/Code/rails/ajax_on_rails/app/views" 

當我嘗試點擊編輯鏈接。它看起來像:

<%= link_to 'Edit', edit_post_path(post, remote: true) %> 

現在,我已經位於應用程序/視圖/職位/ edit.js.erb簡單js.erb文件。該文件不用於響應。看看上面的錯誤消息,格式鍵的值是隻包含:html的數組。如果我創建一個edit.html.js,這工作正常,但不是edit.js.erb文件。

post建議刪除舊的rails.js文件,但我很確定我從來沒有將它包含在這個簡單的應用程序(或者如果我這樣做,在哪裏可以找到它)。

這裏是我的控制器類的樣子:

class PostsController < ApplicationController 
    before_filter :load, :except => [:destroy, :create] 

    def load 
     @posts = Post.all 
    end 

    def index 
     @post = Post.new 
    end 

    def edit 
     @post = Post.find(params[:id]) 
    end 

    def create 
     @post = Post.new(params[:post]) 
     if @post.save 
      flash[:notice] = 'Post was successfully created.' 
      load 
     end 
    end 

    def update 
     @post = Post.find(params[:id]) 
     if @post.update_attributes(params[:post]) 
      flash[:notice] = 'Post was successfully updated.' 
     end 
    end 

    def destroy 
     @post = Post.find(params[:id]) 
     @post.destroy 
     flash[:notice] = 'Successfully destroyed post.' 
     load 
    end 
end 

我不明白爲什麼我的創建和銷燬行動是成功地與js.erb模板響應,但不能編輯。謝謝你的幫助!

回答

1

我相信remote選項應該是link_to的選項,而不是edit_post_path。嘗試將它移到括號外:

<%= link_to 'Edit', edit_post_path(post), remote: true %> 
+0

太棒了,謝謝你的幫助Dylan。這解決了這個問題。你可以在[我的github](https://github.com/wangstabill/100--Ajax-CRUD-Rails-3.1)查看代碼!謝謝! –

+0

酷!一定要接受答案,否則人們不會花費時間回答你的問題。 –