0

我想做完全的 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 文件;创建.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 -%>
4

1 回答 1

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.erbedit.js.erb需要。

我有一个非常干净的项目,专注于在单个页面中执行 AJAX。也许您可以结帐并看看。https://github.com/camsong/AjaxCRUD

于 2012-07-30T10:22:08.933 回答