我的问题很简单,如何让Ruby on Rails应用程序与Vue协同工作.js?
The details
我首先看一下vue-rails
gem,但这会将Vue添加到railsassets资源 管道中,我想使用其他npm包,比如browserify.然后我看一下,browserify-rails
在js文件夹app/assets/javascript/
中启用commonjs.此外,我还计划 for each rails控制器制作一个Vuejs应用程序,并使用vue-resource
和vue-router
访问后端操作(如果这不是一个好方法,请告诉我),因此我在布局中添加了以下内容
<%= javascript_include_tag params[:controller] %>
这将添加一个带有控制器名称的js文件,因此UsersController
将包含该控制器的主vue应用程序的users.js
文件.
然后,为了try 这一点,我用rails构建了一个用户资源,并使其在每个操作中呈现json格式,如下所示
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
这很有效.然后在app/assets/javascript/
文件夹中添加用户.js提供了以下内容
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));
});
}
});
当我在chrome中判断开发控制台时,我看到添加了Vue,但没有看到任何响应,我已经插入了一个用户.顺便说一句,我正在try 使用https://vuejs-rails-yerkopalma.c9users.io/users
url(我在c9.io中开发),只呈现/users/index.html.erb
文件.那么,我的猜测是:
- 我可能用错了
vue-resource
,我指的是传递给get()
函数的url. - 我可能错过了大约
vue-resource
个配置. - 也许我应该用
vue-rails
颗Ruby 加上brwoserify-rails
颗,但我不知道怎么用.任何帮助或指导都将不胜感激.
这是我的application.js
文件(也包括在我的布局文件中)
// 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