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:
- I might be using
vue-resourcein a wrong way, I mean the url passed to theget()function. - I might be missing some
vue-resourceconfiguration. - Maybe I just should use the
vue-railsgem combined withbrwoserify-railsbut 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
.thencall, you have not specified an error handler. You should add one with logging. Additionally, use your web browser's inspector to look at the actual network requests being made to see that it is formed and sent the way you expect it to be.vuejs-railsthe other day and notice your post. So I thought I should inform you that their javascript are outdated.