🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 6 Min Lesezeit
0

Use vite_rails to use Vue SFC(single file component, .vue) in >= Rails7

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Introduction



Rails Hotwire is good but sometimes you want to build a more reactive. Vue.js is a good choice. In addition, Vue.js SFC is an awesome way to manage your components. However, you cannot just use .vue files because browsers don't recognize .vue files. They need to be compiled into JS codes first.



If you already know Vue.js, you probably know that using vite as the bundler to compile .vue files into JS codes is very common in the Vue.js world. There's a gem vite_rails to integrate vite into rails. I'll introduce how to configure vite_rails so you can use .vue files in Rails.






Preparation






If you want to start it from scratch(optional)



importmap is the default way to manage npm packages after Rails 7. However, we don't want to use importmap now, so add --skip-javascript behind the rails new command to opt out of all Javascript codes.




CODE
rails new you_project_name --skip-javascript






Of course, you can keep it if you want. It's just trickier to balance them carefully because they have kind of the same purposes.






Add a simple page



We add a simple index page just for demo




CODE
bundle exec rails g controller pages index






it will create the pages_controller an :index action






Install Vite



Add vite_rails in to the Gemfile




CODE
gem 'vite_rails'






and run




CODE
bundle install
bundle exec vite install






bundle exec vite install will add basic config file and inject some code snippet into the current view template, e.g. in the app/views/layouts/application.html.erb, it will inject these 2 lines




CODE
<%= vite_client_tag %>
<%= vite_javascript_tag 'application' %>








  • vite_client_tag: is for HMR (hot module replacement) so you don't need to refresh the page to the the changes


  • vite_javascript_tag: it will load entrypoints/application.js






Run vite dev server



The vite dev server will transpile your code in real-time. It's required and also helpful for our development so that we can use features like HMR. Run this command in a new terminal session:




CODE
bin/vite dev









Test



To test if the vite_rails is install correctly, you can run rails server by




CODE
rails s






and go to



If you want to run the rails server and vite dev server at the same terminal session. You can install foreman and run foreman start -f Procfile.dev. Procfile.dev was also added by bundle exec vite install





Install Vue



Let's install 2 essential modules:




CODE
npm install vue
npm install @vitejs/plugin-vue








  1. vue: It's Vue.js, of course we need it


  2. @vitejs/plugin-vue: this module let vite recognize SFC and transpile .vue into js



To enable @vitejs/plugin-vue, we need to configure vite. Open vite.config.ts and edit it like this:




CODE
import { defineConfig } from 'vite'
import RubyPlugin from 'vite-plugin-ruby'
import vue from '@vitejs/plugin-vue' // <-------- add this

export default defineConfig({
plugins: [
RubyPlugin(),
vue() // <-------- add this
],
})






It's done and we can try to use SFC in our app!






Mount your first Vue component



Let's create our first Vue SFC, App.vue, and mount it!






create App.vue



Create a file app/frontend/components/App.vue and edit it like this:




CODE
<template>
<div>
<h1>Vue App!</h1>
</div>
</template>

<script setup>

</script>

<style lang="scss" scoped>
h1 {
color: red;
}
</style>






Note that different versions of vite_rails may have a different path for the source folder. The example here is app/frontend/components/App.vue, but your may be app/javascript/components/App.vue. It depends on where is your entries/appliaction.js






Set the mounting point



Then on our index page's erb, we replace everything with




CODE
<%# app/views/pages/index.html.erb %>
<div id="app"></div>






The SFC will be mounted on this div.






import App.vue in application.js



Open app/frontend/entrypoints/application.js. This is the entry point of the javascript program because we wrote <%= vite_javascript_tag 'application' %> in the layout.




CODE
import { createApp } from 'vue'
import App from '../App.vue'

const app = createApp(App)
app.mount('#app')






Import the App.vue and mount it






Test



Go to






Pass data from Rails to Vue components



Rails is very good at fetching the data in the database. You can make an API endpoint returning JSON string and let the Vue component to call that. However, if you just want the Vue components have the data as props when mounting it. For example, we want the App.vue show the text passed to it as props so we modify it as:




CODE
<template>
<div>
<h1>{{ msg }}</h1>
</div>
</template>

<script setup>
defineProps({
msg: String
})
</script>

<style lang="scss" scoped>
h1 {
color: red;
}
</style>






Now our App.vue is expecting props msg to be sent in.



We set a instance variable in PagesController:




CODE
class PagesController < ApplicationController
def index
@msg = "Hello Vue on Rails!"
end
end






We then do a little trick here. Put @msg content on an empty div's data-* attribute, modify app/views/pages/index.html.erb




CODE
<%=
content_tag(
:div,
id: 'appProps',
data: {
props: {
msg: @msg
}
}.as_json
) {}
%>
<div id="app"></div>






it will render the div below on the page.




CODE
<div 
id="appProps"
data-props="{&quot;msg&quot;:&quot;Hello Vue on Rails!&quot;}">
</div>






We can then parse the data-props as a JSON in the application.js and sent the JSON to App.vue




CODE
import { createApp } from 'vue'
import App from '../components/App.vue'

const appProps = document.getElementById('appProps').dataset.props
const app = createApp(App, JSON.parse(appProps))
app.mount('#app')






(This is a trick used by






Conclusion



Vue.js SFC is a very powerful tool. To be honest, I was attracted to it the first time I saw its syntax. Combining HTML, CSS, JS into a single file is so elegant. Using Vue SFC with Rails is really a joy. Hope you like it!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Use vite_rails to use Vue SFC(single file component, .vue) in >= Rails7

Thematisch verwandte Begriffe: viterails, SFCsingle, file, component · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...