0

我正在使用 Rails 3 创建一个基本的留言板应用程序。我希望使用 UJS/jQuery 创建帖子(使用 AJAX 提交帖子并清除表单)。我认为这很简单,但我收到了这个错误:

Rendered posts/create.js.erb (64.5ms)
Completed   in 1771ms

ActionView::Template::Error (undefined local variable or method `posts' for #<#<Class:0x1d681d0>:0x1d66f10>):
    1: $("#posts").html("<%= escape_javascript(render(:partial => posts)) %>");
    2: $("#form").html("<%= escape_javascript(render 'form') %>");
  app/views/posts/create.js.erb:1:in `_app_views_posts_create_js_erb__431255654_15413772__591965598'
  app/controllers/posts_controller.rb:41:in `create'

Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (10.5ms)
Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (53.8ms)
Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (95.0ms)

布局/application.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title><%= @title || controller.action_name %></title>
    <%= stylesheet_link_tag 'style' %>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <%= javascript_include_tag 'jquery.timeago','jquery.watermark', 'rails' %>
    <%= csrf_meta_tag %>
  </head>
  <body>
    <container>
      <%= render "shared/nav" %>
      <section id="content">
        <%- flash.each do |name, msg| -%>
          <%= content_tag :div, msg, :id => "flash_#{name}" %>
        <%- end -%>
        <%= yield %>
      </section>
      <%= render "shared/footer" %>
    </container>
  </body>
</html>

帖子/index.html.erb:

<div id="posts">
  <%= render 'posts' %>
</div>
<%= will_paginate @posts, :previous_label => "previous", :next_label => "next" %>
<div id="form">
  <%= render 'form' %>
</div>

帖子/_posts.html.erb:

<%- for post in @posts -%>
   ...
<%- end -%>

帖子/_form.html.erb:

<%= form_for :post, :remote => true, :url => { :controller => :posts, :action => "create" } do |f| %>

  <%= f.label :title, "title" %>
  <%= f.text_field :title, :placeholder => "title" %>

  <%= f.label :content, "message" %>
  <%= f.text_area :content, :placeholder => "message" %>

  <%= f.submit 'post' %>

<% end %>

帖子/create.js.erb:

$("#posts").html("<%= escape_javascript(render(:partial => posts)) %>");
$("#form").html("<%= escape_javascript(render 'form') %>");

post_controller.rb:

class PostsController < ApplicationController
  before_filter :find_post, :only => [:show, :edit, :update, :destroy]
  respond_to :html, :js

  def index
    @title = "chat"
    @posts = Post.paginate :page => params[:page], :order => "updated_at DESC"
    respond_to do |format|
      format.html
      format.js
    end
  end

  # ...

  def new
    @post = Post.new
    respond_to do |format|
      format.html
      format.js
    end
  end

  def create
    @post = Post.new(params[:post])
    respond_to do |format|
      if @post.save
        flash[:notice] = "Created \"#{@post.title}\""
        format.html { redirect_to(posts_url) }
        format.js
          else
        flash[:error] = "Title, tag and message required"
        format.html { redirect_to(posts_url) }
        format.js
      end
    end
  end

  # ...

  private

  def find_post
    @post = Post.find(params[:id])
    @comments = @post.comments
  end
end

我正在使用 Rails 3.0.0 和 Ruby 1.9.2。不确定是什么问题。

4

2 回答 2

4

posts/create.js.erb

$("#posts").html("<%= escape_javascript(render(:partial => posts)) %>");

应该:

$("#posts").html("<%= escape_javascript(render "posts") %>");

帖子没有定义,所以如果你给它一个字符串,它将评估为部分帖子/_posts.html.erb

另外,对于在 rails 3 应用程序中使用 jQuery 而不是原型,jquery-rails非常棒。

于 2010-09-10T12:27:00.080 回答
0

只要确保,您确实安装了jQuery UJS的东西,对吗?我想你做到了,因为你把它标记为这样,但你在你的帖子中没有提到它。再次,只是确保!

于 2010-09-10T02:51:22.660 回答