Railsに初めて触れる方を対象にしたチュートリアルです
Rails6
から追加されるActionText
を使用するチュートリアルです
まず、Rails6
をインストールします
Rails6
はRuby2.5
以上でないとインストールできません
2.5
より古いバージョンである場合、先にRubyのバージョンを上げておいてください
gem install rails -v '6.0.0beta3'
を実行します
gem install rails -v '6.0.0beta3' --webpack=stimulus
インストールできていればOKです
まずはrails new
を実行し、Rails
アプリのひな型を作成します
rails _6.0.0beta3_ new action_text --webpack=
その後、action_text
へと移動します
cd action_text
先ほどのrails newでsqlite3のインストールがエラーになっている場合は、以下のようにバージョンを修正します
gem 'sqlite3', git: "https://github.com/larskanis/sqlite3-ruby", branch: "add-gemspec"
その後、bundle install
を実行します
bundle install
これでOKです!
rails g scaffold
を使い、CRUDのひな型を作ります
rails g scaffold post title:string content:text
その後、rails db:migrate
でマイグレーションを行います
rails db:migrate
最後に、config/routes.rb
に以下のようにroot
を設定します
Rails.application.routes.draw do
root 'posts#index'
resources :posts
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
あとはrails s
を実行して、localhost:3000
にアクセスできればOKです
まず、bundle exec rails action_text:install
を実行します
bundle exec rails action_text:install
その後、rails db:migrate
を実行します
rails db:migrate
その後、app/models/post.rb
にリレーションを追加します
class Post < ApplicationRecord
has_rich_text :content
end
次に、app/views/posts/_form.html.erb
のcontent
にrich_text_area
を使用します
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.rich_text_area :content %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
最後に、Gemfile
にgem 'image_processing', '~> 1.2'
を追加し、
gem 'image_processing', '~> 1.2'
bundle install
を実行します
bundle install
後は、rails s
でローカルサーバを起動します
rails s
あとは、実際にlocalhost:3000/posts/new
でテキストを入力してみてください
また画像をドラッグ&ドロップで貼り付けることもできます
これでActionText
の導入は完了です!