我想完全完成AJAX CRUD操作,但是当我点击某些操作时它会告诉我,
Template missing
这是我的控制器:
before_filter :load
def load
@posts = Post.all
@post = Post.new
端
def index
end
def create
@post = Post.new(params[:post])
if @post.save
flash[:notice] = "Successfully created post."
@posts = Post.all
end
端
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
flash[:notice] = "Successfully updated post."
@posts = Post.all
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
flash[:notice] = "Successfully destroyed post."
@posts = Post.all
end
这是我的application.erb.html:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'%>
<%= javascript_include_tag 'rails' %>
</head>
<body>
<div id="container">
<div id="flash_notice" style="display:none"></div>
<%= yield %>
</div>
</body>
我的index.html:
<h1>Listing posts</h1>
<div id="post_form"><%= render :partial => 'form' %></div>
<div id="posts_list"><%= render :partial => "posts" %></div>
表格部分:
<table>
<tbody><tr>
<th>Title</th>
<th>Content</th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
<td><%= link_to "Edit", edit_post_path(post), :remote => true %></td>
<td><%= link_to "Destroy", post, :remote => true, :confirm => 'Are you sure?', :method =>:delete %></td>
</tr>
<% end %>
</tbody>
</table>
我为所有动作创建了js.erb文件; create.js.erb:
<% if @post.errors.any? -%>
/*Hide the flash notice div*/
$("#flash_notice").hide(300);
/*Update the html of the div post_errors with the new one*/
$("#post_errors").html("<%= escape_javascript(error_messages_for(@post))%>");
/*Show the div post_errors*/
$("#post_errors").show(300);
<% else -%>
/*Hide the div post_errors*/
$("#post_errors").hide(300);
/*Update the html of the div flash_notice with the new one*/
$("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
/*Show the flash_notice div*/
$("#flash_notice").show(300);
/*Clear the entire form*/
$(":input:not(input[type=submit])").val("");
/*Replace the html of the div post_lists with the updated new one*/
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
<% end -%>
/*Hide the div post_errors*/
$("#post_errors").hide(300);
/*Update the html of the div flash_notice with the new one*/
$("#flash_notice").html("<%= escape_javascript(flash[:notice])%>");
/*Show the flash_notice div*/
$("#flash_notice").show(300);
/*Clear the entire form*/
$(":input:not(input[type=submit])").val("");
/*Replace the html of the div post_lists with the updated new one*/
$("#posts_list").html("<%= escape_javascript( render(:partial => "posts") ) %>");
<% end -%>
答案 0 :(得分:2)
您似乎只有create.js.erb
。因为你有
<td><%= link_to "Edit", edit_post_path(post), :remote => true %></td>
<td><%= link_to "Destroy", post, :remote => true, :confirm => 'Are you sure?', :method =>:delete %></td>
因此需要destroy.js.erb
,edit.js.erb
。
我有一个非常干净的项目专注于在单个页面中进行AJAX。也许你可以结帐并看看。 https://github.com/camsong/AjaxCRUD