How to make rails work with vue.js?

Yerko Palma

Well my question is simple, how to make a Ruby on Rails app work with Vue.js?

The details

I first look at the vue-rails gem, but that add Vue to the rails asset pipeline, and I want to work with other npm packages, like browserify. Then I look to that, browserify-rails that enable commonjs in the js folder app/assets/javascript/. Also I'm planning to make a Vuejs app for every rails controller, and access to backend actions with the vue-resource and vue-router (if this isn't a good approach please let me know), so I added the following line to my layout

<%= javascript_include_tag params[:controller] %>

That will add a js file with the name of the controller, so the UsersController will have the users.js file with the main vue app for that controller.

Then, to try this I scaffolded a Users resource with rails, and make it render json format in every action, like this

def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
end

That is working fine. Then in the app/assets/javascript/ folder I add users.js with the following content

var Vue = require('vue');
Vue.use(require('vue-resource'));

new Vue({
    ready: function(){
        this.$http.get('users.json').then(function(response){
            console.log(JSON.stringify(response));
        });
    }
});

And when I inspect dev console in chrome, I see that Vue is added, but I don't see any response, and I had already inserted one user. By the way, I'm trying with the https://vuejs-rails-yerkopalma.c9users.io/users url (I'm developing in c9.io) and only the /users/index.html.erb file is rendered. So, waht are my guesses:

  1. I might be using vue-resource in a wrong way, I mean the url passed to the get() function.
  2. I might be missing some vue-resource configuration.
  3. Maybe I just should use the vue-rails gem combined with brwoserify-rails but I just don't know how. Any help or guideline would be appreciate.

Here is my application.js file (is also included in my layout file)

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
Yerko Palma

I managed to make this work with Vue.js.

first I added an el propertie to the object passed to Vue constructor, and I started to get an error saying that body element was not defined. Obviously the body element is always present in an html file, so I though it was a problem about when the Vue instances were created. So I simply wrapped all on a ready event.

$(document).on('ready page:change', function() {
    var Vue = require('vue');

    Vue.use(require('vue-resource'));       

    new Vue({
        el: 'body',
        data: {
            users: []
        },
        ready: function(){
            this.$http.get('users.json').then(function(response){

                console.log(JSON.stringify(response.data));
                this.users = response.data;
            }, function(response){
                console.log("fail");
                console.log(JSON.stringify(response));
            });                
        }
    });        
});

And that works fine!

Because I'm using turbolinks, I also included the page:change event. And in my view index.html.erb I have access to Vue instance. So this make this case working and it's fine for this particular question, however I now have another issue working with .vue components, maybe I'll open another question about it.

Notice that in my index.html.erb view I have access to the Vue instance defined in users.js file in the assets/javascript/ folder, but that I have not access to Vue or any of the js modules loaded with browserify from the views folder

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make this JS link work?

From Dev

how to make add this work with turbilinks rails 4

From Dev

How to make spree, rails and liqpay to work together?

From Dev

How to make Rails 4.2 work with Postgres Jsonb?

From Dev

how to make add this work with turbilinks rails 4

From Dev

How to make jQuery work in Rails 5

From Dev

Can't make Emblem.js work with Rails

From Dev

How to make a reusable vue js component?

From Java

How to make lodash work with Angular JS?

From Dev

How to make the linker in Sails js work with Jade?

From Dev

how to make validates inclusion true or false work in testing rails?

From Dev

How to make .html.erb file work outside of Rails?

From Dev

How to make Minitest-Reporters work with Rails 5?

From Dev

How do you make a rails multi select dropdown work

From Dev

How can I make this script to kill Rails processes work better?

From Dev

Rails - make link work with ajax

From Java

How vue.js cli command "npm run serve" work

From Java

How to make custom directive in Vue.js 3?

From Dev

How to make Vue js directive working in an appended html element

From Dev

How can I make a list and add sort function with Vue JS?

From Dev

Rails 4 welcome wizard, how to correct this code to make it work for rails 4?

From Dev

How to make a countdown in ruby on rails with flipclock.js?

From Dev

How to make minimal example of matter.js work?

From Dev

How to make TypeScript work with SystemJS? (issue with .js extension)

From Dev

Socket.io and express.js, how to make it work?

From Dev

How can I make Reveal.js work with a presenter remote?

From Dev

how to make .ebextensions work when deploying a node js application?

From Dev

How to make a chart in D3 work in node.js

From Dev

How to make JS & CSS inline work for eBay template?

Related Related

  1. 1

    How to make this JS link work?

  2. 2

    how to make add this work with turbilinks rails 4

  3. 3

    How to make spree, rails and liqpay to work together?

  4. 4

    How to make Rails 4.2 work with Postgres Jsonb?

  5. 5

    how to make add this work with turbilinks rails 4

  6. 6

    How to make jQuery work in Rails 5

  7. 7

    Can't make Emblem.js work with Rails

  8. 8

    How to make a reusable vue js component?

  9. 9

    How to make lodash work with Angular JS?

  10. 10

    How to make the linker in Sails js work with Jade?

  11. 11

    how to make validates inclusion true or false work in testing rails?

  12. 12

    How to make .html.erb file work outside of Rails?

  13. 13

    How to make Minitest-Reporters work with Rails 5?

  14. 14

    How do you make a rails multi select dropdown work

  15. 15

    How can I make this script to kill Rails processes work better?

  16. 16

    Rails - make link work with ajax

  17. 17

    How vue.js cli command "npm run serve" work

  18. 18

    How to make custom directive in Vue.js 3?

  19. 19

    How to make Vue js directive working in an appended html element

  20. 20

    How can I make a list and add sort function with Vue JS?

  21. 21

    Rails 4 welcome wizard, how to correct this code to make it work for rails 4?

  22. 22

    How to make a countdown in ruby on rails with flipclock.js?

  23. 23

    How to make minimal example of matter.js work?

  24. 24

    How to make TypeScript work with SystemJS? (issue with .js extension)

  25. 25

    Socket.io and express.js, how to make it work?

  26. 26

    How can I make Reveal.js work with a presenter remote?

  27. 27

    how to make .ebextensions work when deploying a node js application?

  28. 28

    How to make a chart in D3 work in node.js

  29. 29

    How to make JS & CSS inline work for eBay template?

HotTag

Archive