“模板丢失” Rails 3.1 尝试渲染时出错
我正在尝试将 Ajax 与 CRUD 结合使用。我正在关注这个 教程。
我看到此错误:
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) %>
现在,我有一个简单的 js.erb 文件位于 app/views/posts/edit.js.erb 中。该文件不用于响应。查看上面的错误消息,:formats 键的值是只包含:html 的数组。如果我创建一个 edit.html.js,这可以正常工作,但不能创建 edit.js.erb 文件。
这篇帖子建议删除旧的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 模板,但不能编辑。感谢您的帮助!
I'm trying to use Ajax with my CRUD. I'm following this tutorial.
I see this error:
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"
when I try to click the edit link. It looks like:
<%= link_to 'Edit', edit_post_path(post, remote: true) %>
Now, I have a simple js.erb file located in app/views/posts/edit.js.erb. This file is not used for a response. Looking at the above error message, the :formats key's value is the array that only contains :html. If I create a edit.html.js, this works fine but not the edit.js.erb file.
This post recommends removing the old rails.js file, but I'm quite sure I never included it in this simple app (or where to find it if I did).
Here's what my controller class looks like:
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
I don't understand why my create and destroy actions are successfully responding with js.erb templates, but not edit. Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
remote
选项应该是link_to
的选项,而不是edit_post_path
的选项。尝试将其移到括号之外:I believe the
remote
option should be an option forlink_to
, not foredit_post_path
. Try moving it outside the parentheses: