// "-webpack=vue"Vue en option.js disponible
$ rails new <Nom de l'application> -webpack=vue
//Nom de colonne: nom Type de données: texte
$ rails g model sample name:text
db/migrate/20200627045139_create_sample.rb
class CreateSample < ActiveRecord::Migration[6.0]
def change
create_table :sample do |t|
t.text :name, null: false, default: ""
end
end
end
$ rails db:create //Création de base de données
$ rails db:migrate //Mise en œuvre de la migration
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
config/routes.rb
Rails.application.routes.draw do
root to: 'home#index'
end
app/views/home/index.html.erb
<%= javascript_pack_tag 'hello_vue' %>
<%= stylesheet_pack_tag 'hello_vue' %>
app/javascript/packs/hello_vue.js
import Vue from 'vue'
import App from '../app.vue'
document.addEventListener('DOMContentLoaded', () => {
const el = document.body.appendChild(document.createElement('hello'))
const app = new Vue({
el,
render: h => h(App)
})
console.log(app)
})
app/javascript/app.vue
<template>
<div id="app">
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
data: function () {
return {
message: "Hello Vue!"
}
}
}
</script>
<style scoped>
p {
font-size: 2em;
text-align: center;
}
</style>
◆ before_action --Définissez une méthode et définissez-la sur `` before_action ''
login_controller.rb
class LoginController < ApplicationController
before_action :set_answer
def set_answer
@sample = "Hello World!"
end
end
◆ rescue_from
app/controller/application_controller.rb
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :rescue404
end
Webpacker::Manifest::MissingEntryError in Home#index
$ yarn
$ bin/yarn
$ webpack
$ webpack
Error: vue-loader requires @vue/compiler-sfc to be present in the dependency tree.
$ npm remove vue-loader
$ npm install --save [email protected]
$ yarn add [email protected]
Sprockets::Rails::Helper::AssetNotFound in Home#index
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
app/views/layouts/application.html.erb
<!-- javascript_include_Supprimer le slogan-->
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
Recommended Posts