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 ファイルを作成しました。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 -%>
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 に答える