带有 jQ​​uery 的简单 UJS 无法在 Rails 3 中工作

发布于 2024-09-18 16:31:34 字数 4048 浏览 6 评论 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)

layouts/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>

posts/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/_posts.html.erb:

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

posts/_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 %>

posts/create。 js.erb:

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

posts_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。不确定是什么问题。

I'm creating a basic message board app with Rails 3. I want posts to be created with UJS/jQuery (submit the post with AJAX and clear the form). I thought this would be simple but I'm getting this error:

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)

layouts/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>

posts/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/_posts.html.erb:

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

posts/_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 %>

posts/create.js.erb:

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

posts_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

I'm using Rails 3.0.0, and Ruby 1.9.2. Not sure what the issue is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-09-25 16:31:34

posts/create.js.erb

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

应该是:

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

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

除了使用 jQuery jquery-rails 非常棒,而不是 Rails 3 应用程序中的原型。

in posts/create.js.erb

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

should be:

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

There's no definition for posts, so if you give it a string, it will evaluate to the partial posts/_posts.html.erb

Aside, for using jQuery instead of prototype in a rails 3 application, jquery-rails is awesome.

-柠檬树下少年和吉他 2024-09-25 16:31:34

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

Just making sure, you did install the jQuery UJS stuff right? I imagine you did, since you tagged it as such, but you made no mention of it in your post. Again, just making sure!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文