Google Polymer with rails 4

前端 未结 5 985
长情又很酷
长情又很酷 2021-02-04 20:52

I have build a Ruby on rails app.I want to use polymer with my ruby on rails app. Cam anybody suggest some good resources to learn polymer with rails ?

Is it efficient t

5条回答
  •  Happy的楠姐
    2021-02-04 21:08

    I was first trying to use web components with bower-rails and emcee.

    But I noticed that bower-rails isn't necessary, you just need to configure .bowerrc and bower.json then run bower install.

    Also, emcee is no longer active which forces you to use an older version of sprockets.

    It turns out polymer-rails has the same functionality as emcee.

    Here is my current setup

    Gemfile

    gem 'polymer-rails'
    

    install and initialize

    bundle install
    rails g polymer:install
    

    app/views/layouts/application.html.erb

    
      
        ...
        <%= html_import_tag 'application'%>
        ...
    

    Download the bower.json file from elements.polymer-project.org and put it in the root of your app then run the command

    bower install
    

    Now your components should be in vendor/assets/components. Find all the .html files to include in the manifest. Some of the .html files are not the same name as the directory so it is a little tedious.

    app/assets/components/application.html.erb

    //= require polymer/polymer
    //= require paper-scroll-header-panel/paper-scroll-header-panel
    //= require paper-toolbar/paper-toolbar
    //= require paper-tabs/paper-tabs
    //= require paper-drawer-panel/paper-drawer-panel
    //= require paper-icon-button/paper-icon-button
    //= require iron-icons/iron-icons
    //= require paper-card/paper-card
    //= require paper-button/paper-button
    ...
    

    Now make sure sprockets loads the assets

    config/initializers/assets.rb

    Rails.application.config.assets.paths << Rails.root.join('vendor', 'assets', 'components', '*')
    

    Here is an example of how to use a component

    
      
    This is an example of cards with polymer
    OK

    You can use polymer-elements-rails which has a lot of the components already and you can skip the bower.json, bower install, and load assets steps. The only problem is I had a hard time finding out what the require paths were and some of the components were missing (like google-apis). Hope this helps anyone trying to use polymer with rails!

提交回复
热议问题