I am trying to learn how to create a Rails 5.1 application using Vuejs, created using the Webpacker gem.
$ rails new myvueapp --webpack=vue
How do I pass instance variable data back on Thursday between Vue components to get my data to / from the database?
Say, for example, that I have a User model with the Username and Email fields. Is there a simple example of how to pass an instance variable, @user, data from a controller to a component?
I believe that there are two ways to do this:
(1) There is in the controller render :json => @user, and then use something like the Fetch or Axios API to retrieve the data in the .vue component.
-or-
(2) Use something like an example <%= javascript_pack_tag 'hello_vue' %>along with <%= content_tag ... %>in a view.
When I do this example <%= javascript_pack_tag 'hello_vue' %>, I see "Hello Vue!". from the data in the component "message" variable, but I find it difficult to find an example of the data of instance variables, for example: @user data that is sent and exited from Rails.
Any examples of how to transmit @user data would be much appreciated.
Thank.
source
share