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

酷!一定要接受答案,否则人们不会花费时间回答你的问题。 –