diff --git "a/docs/\351\226\213\347\231\272\350\200\205\343\203\226\343\203\255\343\202\2600213" "b/docs/\351\226\213\347\231\272\350\200\205\343\203\226\343\203\255\343\202\2600213" index 4d36c95..024a196 100644 --- "a/docs/\351\226\213\347\231\272\350\200\205\343\203\226\343\203\255\343\202\2600213" +++ "b/docs/\351\226\213\347\231\272\350\200\205\343\203\226\343\203\255\343\202\2600213" @@ -1,78 +1,73 @@ -こんにちは。フルスタックエンジニアのミラーです。 +こんにちは。フルスタックエンジニアのミラーです。ブラニューで、RailsとVue.jsで[EAT](start.eat.auto)の開発をしています。 +Vue.jsでは複雑でスケーラブルなSPAを作るのに使えますが、従来的なサーバーレンダーされるアプリケーションに動的アクションを付けるのにも便利です。 -Vue.js can be used for building scalable, complex single page applications, which is generally what I blog about. However, another value use case is as a utility library to add some dynamic functionality to server rendered applications. -This article will look at how we can use Vue.js to add some client side form validation in a Rails application. +この記事でRailsアプリケーションにVue.jsでフォームバリデーションをします。 -## Setup +ソースコードはこちらで。https://github.com/lmiller1990/vue-rails-form-validation -Scaffold a new application using rails new blogger. A future article will look at how to use the new webpacker gem to bundle the application using webpack; for simplicity, I’ll stick with the usual Rails asset pipeline for now. -After creating the Rails app, add the following to your Gemfile: +`rails new blogger`で新しいRailsアプリケーションを作成します。Vue.jsを`webpacker`で追加するのも可能ですが、この記事でデフォルトのアセットパイプラインで追加します。 -gem 'vuejs-rails' +`Gemfile`で`vuejs-rails`を追加します。 -and bundle install. This adds Vue to the asset pipeline. Lastly, we need to include it in assets/javascripts/application.js: +`gem 'vuejs-rails` -//= require vue +そして`bundle install`。これでVue.jsをアセットパイプラインに追加します。最後に`assets/javascripts/application.js`で: -## Creating the controller and model +`//= require vue` -This will be a simple form — it allows the user to create a Blog, which has a title and body, with minimum lengths of 5 and 10. Firstly, create the controller and actions: +### コントローラとモデルを作成 +アプリケーション作成は簡単です。ただのフォームで`blog`を作ります。`blog`には`title`, `body`があります。コントローラを作ります。 -``` -rails generate controlls blogs index create new -``` +`rails generate controller blogs index create new` -and the model: +とモデル: -``` -rails generate model blog title:string body:string -``` +`rails generate model blog title:string body:string` + +と、モデルとデータベースをマイグレートします: -migrate your database with `bin/rails db:migrate`. +`bin/rails db:migrate` -## Adding the routes +### ロートを追加 +`config/routes.rb`を更新します: -Creating the routes is easy. Update `routes.rb`: ``` Rails.application.routes.draw do resources :blogs end ``` -## Creating the controller actions - -For the purpose of this demo, we only need three actions: `create`, `new` and `index`. We will also whitelist the params as is usual in Rails controllers. +### コントローラのアクションを作成 -```rb +``` class BlogsController < ApplicationController -def index + def index @blogs = Blog.all -end - -def create - @blog = Blog.new(blog_params) - if @blog.save! - render json: {}, status: :created - else - render json: {}, status: :internal_server_error - end -end - -def new + end + def create + @blog = Blog.new(blog_params) + if @blog.save! + render json: {}, status: :created + else + return :internal_server_error + end + end + def new @blog = Blog.new(title: 'Title', body: '') -end - -private + end -def blog_params + private + def blog_params params.require(:blog).permit(:title, :body) end end +``` -## Creating the view +一般的なRailsのコントローラです。これからVueを使います! -This is where things get a bit interesting. We need to pass the data from @blog to the Vue instance (which we have not made yet). Update app/views/blogs/new.html.erb, which should have been generated when you scaffolded the controller earlier: +### Viewを作成を作成 +コントローラの`new`アクションで定義された`@blog`をViewを作成のフォームに渡さないといけません。`app/views/blogs/new.html.erb`を更新します: ```
Find me in app/views/blogs/new.html.erb
-<%= content_tag :div, - id: 'blog-form-vue', - data: { - blog: @blog.to_json() - } do %> +そして、フォームも更新します:(`views/blogs/new.html.erb`)。