You may need to install yarn
(Javascript package manager) before you can create Rails apps that involve Javascript frontends.
For the WSL run the following commands:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
The password you are prompted for is your WSL root password.
Create a new app: rails new vue_todo --webpack=vue
If you have an existing Rails app you can add Vue support by running: rails webpacker:install:vue
Rename the generated app/javascript/packs/hello_vue.js
to better fit with your project. (For example: todo_vue.js
) And then ensure that you include the associated javascript and stylesheet packs in your application layout:
<%= javascript_pack_tag 'todo_vue' %>
<%= stylesheet_pack_tag 'todo_vue' %>
Use Rails to create the following:
rails g scaffold ToDo description:string done:boolean
rails g controller pages home about
Remove the scaffold CSS:
rm app/assets/stylesheets/scaffold.scss
Update the routes in config/routes.rb
:
Rails.application.routes.draw do
resources :to_dos
root to: 'pages#home'
get 'about', to: 'pages#about', as: 'about'
end
Add the vue app div to the home view (app/views/pages/home.html.erb
):
<h1>Your To Do List</h1>
<div id="vue-todo-app"></div>
If you are going to be using the rails routing system to navigate around (i.e. you aren't building a SPA) you'll want to add support for turbolinks.
Command line: ./bin/yarn add vue-turbolinks
In your js file that loads your view app:
import TurbolinksAdapter from 'vue-turbolinks';
import Vue from 'vue';
import App from '../app.vue';
Vue.use(TurbolinksAdapter);
document.addEventListener('turbolinks:load', () => {
const el = document.getElementById('vue-todo-app');
if (el !== null) {
new Vue({
el,
render: h => h(App)
});
}
});
You'll still need to run the Rails server:
rails s
But you'll also want the webpack dev server running in another terminal:
./bin/webpack-dev-server
-
Install the following plugins: Prettier, Eslint, Vetur
-
Install eslint and its vue plugin:
yarn add -D eslint eslint-plugin-vue
-
Create a
.eslintrc.js
config file in the project root:module.exports = { env: { browser: true, es6: true }, extends: ["eslint:recommended", "plugin:vue/recommended"], rules: { // override/add rules settings here, such as: // 'vue/no-unused-vars': 'error' } };
-
Update the VS Code
settings.json
file:"vetur.validation.template": false, // Allow eslint / eslint-plugin-vue handle the linting. "vetur.format.defaultFormatter.html": "js-beautify-html", "eslint.validate": [ "javascript", "javascriptreact", { "language": "vue", "autoFix": true } ], "[javascript]": { "editor.formatOnSave": true, }, "[vue]": { "editor.formatOnSave": true, },
Add bulma to the project Gemfile
:
gem 'bulma-rails', '~> 0.7.4' # Or whatever happens to be the latest version.
Run: bundle install
Rename app/assets/stylesheets/application.css
to applications.scss
and import bulma:
@import 'bulma';
Restart the rails sever and see if the bulma styles have been applied.
Install the Vue Deve Tools for Chrome or Firefox.
Add babel polyfill: ./bin/yarn add -D @babel/polyfill
Load this from your app/javascript/packs/application.js
file:
import "@babel/polyfill";
Hi, how to run eslint when browser load page ?