Well, my question is simple, how to make a Ruby on Rails application with Vue.js?
Details
I will look at the vue-rails gem first, but add Vue to the rails resource pipeline, and I want to work with other npm packages, such as a browser. Then I look at this, browserify-rails , which allow commonjs to be used in the js app/assets/javascript/ folder. I also plan to make a Vuejs application for each rails controller and access the backend actions using vue-resource and vue-router (if this is not a good approach, let me know), so I added the following line to my layout
<%= javascript_include_tag params[:controller] %>
This will add a js file with the name of the controller, so UsersController will have a users.js file with the main vue application for this controller.
Then, to try this, I ran the Users resource with rails and make it render the json format in every action, like this
def index @users = User.all respond_to do |format| format.html
It works great. Then in the app/assets/javascript/ folder, I add users.js with the following contents
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 check the dev console in chrome, I see that Vue has been added, but I don't see any answer, and I already inserted one user. By the way, I'm trying to use the url https://vuejs-rails-yerkopalma.c9users.io/users (I'm developing in c9.io), and only the file /users/index.html.erb . So waht are my guesses:
- I could use
vue-resource incorrectly, I mean that the URL is passed to the get() function. - Perhaps I am missing the
vue-resource configuration. - Maybe I just need to use the
vue-rails gem in combination with brwoserify-rails , but I just don't know how to do this. Any help or recommendations would be appreciated.
Here is my application.js file (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 vendor/assets/javascripts directory can be referenced here using a relative path. // // It 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