- rails new PostVueRails --database=postgresql
- rails webpacker:install:vue
- yarn add vue-turbolinks
- Add vue app component in application.js and when route not match from rails routes the it'll redirecti to vue_application
- Create manually folder in controllers api/v1
- Create post controller under api/v1 rails generate controller api/v1/posts --skip-template-engine
- Create javascript/components folder and create components as controller wise
-
yarn add axios
-
import axios from "axios"; in components (In which do you want to call API)
-
Use: axios.method_name('path_name').then(response => { set_response }) method_name: Get, Post, Delete, Put path_name: In which Api do you want to call
-
While updating, deleting, creating data you'll get "Can't verify CSRF token authenticity." Disable CSRF token. Add skip_before_action :verify_authenticity_token in application_controller.rb
-
Other option for "Can't verify CSRF token authenticity." get csrf token and pass in header in API call
- yarn add vue-router
- Make seprate file for vue routing. app/javascript/packs/router.js -> create router.js
- import vue-router and register it.
- import router.js in main.js and appy it in creating vue component
https://medium.com/canariasjs/vue-api-calls-in-a-smart-way-8d521812c322
Repositories are classes or components that encapsulate the logic required to access data sources. -> Centralize common data access functionality
- Create repository.js in javascript/packs folder
- Create common code for API call and then import repository.js file and use code to other file.
- axios({ url: path of request API, methid: Which method do you want to perform, data: pass params, headers: pass csrf token })
- Call this from other repository file
https://www.youtube.com/watch?v=Ah5SaN1linA
- Add gem 'active_model_serializers' in gem file.
- rails g serializer post --> give model name
- Define attributes in postserializer and custome attribute define with def function
- render json: @post --> called postserializer
https://github.com/jsonapi-serializer/jsonapi-serializer 5) gem 'jsonapi-serializer' 6) Call serializer Serializer_Name.new(model_object).serializable_hash.to_json 7) Custom attributes add with attribute keyword
we’ll start a separate webpack server. We don’t need to do this, but it will make JS compile times much faster and also auto reload the page on JS change, so it is nice to have. Open a separate console, go into the project directory and type: