Rails的3.2.2不执行RJS [英] Rails 3.2.2 not executing rjs

查看:167
本文介绍了Rails的3.2.2不执行RJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这本书的务实敏捷Web开发使用Rails第4版的,但我使用Rails 3.2.2,而不是3.0.5所建议的书:

I'm following the book Pragmatic Agile Web Development With Rails 4th Edition, BUT I'm using Rails 3.2.2 instead of 3.0.5 as recommended in the book:

~$ ruby -v
ruby 1.9.3p125 (2012-02-16) [i686-linux]
~$ rails -v
Rails 3.2.2

我,包括AJAX重绘车无需重新加载页面时卡住了。这里是创建line_items_controller.rb行动:

I got stuck when including AJAX to redraw the Cart without reloading the page. Here is the create action in line_items_controller.rb:

def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(store_url) }
        format.js 
        format.json { render json: @line_item, status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

和这里是我的RJS文件create.js.rjs(在应用程序/视图/ line_items):

And here is my RJS file create.js.rjs (under app/views/line_items):

page.alert('NO PROBLEM HERE')
page.replace_html('cart', render(@cart))

然而,当我点击启动该操作的按钮:

However, when I click the button that starts this action:

<%= button_to 'Add to Cart', line_items_path(:product_id => product), :remote => true %>

我得到在开发日志中出现以下错误:

I get the following error in the development log:

ActionView::MissingTemplate (Missing template line_items/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/home/me/src_rails/depot/app/views"
):
  app/controllers/line_items_controller.rb:47:in `create'

如果我改变create.js.rjs到create.js.erb的文件名,问题得到解决:

If I change the filename of create.js.rjs to create.js.erb, the problem is corrected:

Rendered line_items/create.js.erb (0.4ms)

但什么也没有发生在视图中....甚至没有警报。 我在想什么? file.js.erb和file.js.rjs之间的区别是什么?

but nothing happens in the view.... not even the alert. What am I missing? What is the difference between file.js.erb and file.js.rjs?

推荐答案

看起来 RJS 一直的删除作为默认和Rails 3.1的。你可以拿回来通过安装原型轨道宝石,但我认为你应该使用jQuery,这是新的默认。

It looks like rjs has been removed as the default as of Rails 3.1. You could get it back by installing the prototype-rails gem, but I think you should just use jQuery, which is the new default.

至于你的code,它不工作的原因是,它是一个 RJS 模板是跨preTED为的.js .erb ,而这很可能只是产生无效的JavaScript(你应该看到在你的浏览器中的JavaScript控制台错误)。用来设置变量你,你会用它来编写Ruby的code时 RJS 模板操纵你的网页。在 .js.erb 模板,它的工作原理更像是在你的 .html.erb 的意见。你写的实际JavaScript中,使用与红宝石嵌入&LT; %%&GT; 标签。因此,在 create.js.erb 的code应该是这个样子:

As for your code, the reason it's not working is that it's an rjs template being interpreted as .js.erb, and this is likely just producing invalid JavaScript (you should see errors in the JavaScript console in your browser). An rjs template used to set the page variable for you, and you would write Ruby code using it to manipulate your page. In a .js.erb template, it works more like in your .html.erb views. You write actual JavaScript, with Ruby embedded using <% %> tags. So the code in create.js.erb should look something like this:

 alert('NO PROBLEM HERE');
 $('#cart').html("<%= escape_javascript(render(@cart)) %>");

这篇关于Rails的3.2.2不执行RJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆