Open9
rails engine
ブログアプリを作る。
$ rails _6.0.6_ new engine_sample --skip-javascript
$ cd engine_sample
$ rails g controller top show
$ rails g scaffold authors name:string
$ rails g scaffold articles title:string body:text author:references
$ rails db:migrate
config/routes.rb
Rails.application.routes.draw do
resources :articles
resources :authors
root 'top#show'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>EngineSample</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
</head>
<body>
<header>
<nav>
<ul>
<li><%= link_to('Home', root_path) %></li>
<li><%= link_to('Articles', articles_path) %></li>
<li><%= link_to('Authors', authors_path) %></li>
</ul>
</nav>
</header>
<div class='body'>
<%= yield %>
</div>
</body>
</html>
app/assets/stylesheets/common.scss
header {
background-color: #eee;
nav {
display: inline-block;
ul {
list-style: none;
margin: 0;
padding: 0;
li {
float: left;
a {
display: block;
padding: 10px;
text-decoration: none;
color: #000;
&:hover {
background-color: #eee;
}
}
}
}
}
}
やりたいこと
- rails engineでauthorモデルを使ったページを作成
- rails engineで作ったページのレイアウトを組み込んだ側のものを利用
- rails engineを別のリポジトリで管理
- rails engineだけでテスト
- rails engineだけで開発
rails plugin new special --mountable
ようやく表示できた…。
結構詰まったぞ、これだけやるのに。
やりたいこと
- rails engineでauthorモデルを使ったページを作成
- rails engineで作ったページのレイアウトを組み込んだ側のものを利用
-
rails engineを別のリポジトリで管理
- engineはGemとして管理されているので、リポジトリURLを指定して、そこから取り込むこともできます。 -
rails engineだけでテスト
- 本体とは独立してテストが書けます。
- 別途、依存Gemを追加する必要はあります。 -
rails engineだけで開発
- エンジンのルートディレクトリでrails sをすれば動きます。
ハマったところ
-
rails s
で起動しなかった
- .gemspecにspec.add_development_dependency 'puma'
などを足す必要がありました。 - デバッガが使えなかった
- .gemspecにspec.add_development_dependency 'pry-byebug'
などを足す必要がありました。 - migrationファイルで
references
が使えなかった
- to_tableでモジュール名のプレフィクスのついたテーブル名を指定する必要がありました。 - エンジンを組み込んだときにエンジンのcssが見つからなかった
- blorgh/lib/blorgh/engine.rb でapplication.css/jsを追加する必要がありました。
イケそうな雰囲気を醸し出してきたのに…作業場所を混乱させてしまった…。
もう一回作り直してみよう…。
$ rails _6.0.6_ new engine_sample2 --skip-javascript
$ cd engine_sample2
$ rails g controller top show
$ rails g scaffold users name:string
$ rails g scaffold projects title:string body:text user:references
$ rails db:migrate
仕切り直して、納得いくくらいまでできた。