Skip to content

Instantly share code, notes, and snippets.

@gabytabs
Created June 9, 2017 05:11
Show Gist options
  • Select an option

  • Save gabytabs/989e34f16e40dc1f88ebea76f5316eda to your computer and use it in GitHub Desktop.

Select an option

Save gabytabs/989e34f16e40dc1f88ebea76f5316eda to your computer and use it in GitHub Desktop.
Vue on rails
<%= content_tag :div,
id: "post-index",
data: {
posts: @posts.to_json(except: [:id, :created_at, :updated_at])
} do %>
<h1> Posts </h1>
<div v-for="post in posts">
<h2>{{post.tweet}}</h2>
</div>
<% end %>
import Vue from 'vue/dist/vue.js'
import TurbolinksAdapter from 'vue-turbolinks'
document.addEventListener('turbolinks:load', () => {
var element = document.getElementById("post-index")
if (element != null) {
var posts = JSON.parse(element.dataset.posts)
var post_index = new Vue({
el: element,
mixins: [TurbolinksAdapter],
data: function(){
return {
posts: posts
}
}
})
}
})
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment