How to use local or instance variable in ruby code in coffeescript in haml template

萝らか妹 提交于 2019-12-02 08:12:42

Coffee Script and Rails engine Relation?

There is no relation between rails and coffee script and nothing in rails can be accessed in coffee script.

What can I do then ?

Just create def.js.erb file in your view folder and write your javascript code. in this file you can access all instance variable in method.

How does it calls method.js.erb ?

use this:

respond_to do |format|
  format.js
end

this will call method.js.erb file instead of method.html.erb file.

Hope It helps

The short answer is, you can't. Ruby runs on your server, but any JavaScript (which CoffeeScript emits) you put in your template will run in the browser.

Anything you put inside a coffee: block must be valid CoffeeScript. As others have pointed out in comments, if you want Rails to do something via JS, you need Ajax.

For example, to create a Model with Ajax, set up a controller with a create action and post to it in JS.

For more details, see the excellent Working with JavaScript in Rails tutorial.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!