简体   繁体   中英

writing a ruby gem that adds a js method to rails

I want to write a ruby gem, when installed in a rails app, introduces the javascript method .foo() So I will be able to do $(body).foo() or $(any-dom-element).foo()

How do I go about doing this?

So far what I've done:

Added this to my main rails app:

gem "foo", path: "~/foo"

Wrote the code for the foo() method

// file ~/foo/lib/assets/javascripts/foo.js
// https://learn.jquery.com/plugins/basic-plugin-creation/
$.fn.foo = function() {
    this.css( "color", "green" );
};

//$( "a" ).foo(); // Makes all the links green.

Added a require statement in the main rails app

// app/assets/javascripts/application.js
//= require foo

Alright I figured it out. Halfway through doing it, I found a great article about it.

instead of writing all the steps I did, I'm going to post a link to the article which gives step by step instructions

http://geekhmer.github.io/blog/2015/01/02/develop-your-own-gem-and-gemify-your-own-assets-using-rails-engine/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM