Tag: Rails/プラグイン/チュートリアル
bundle exec rails plugin new ~/work/blorgh --mountable --skip-bundle
bundle install --path=vendor/bundle
ポイント
gem 'blorgh', path: "vendor/engines/blorgh"
require "blorgh/engine" module Blorgh end
module Blorgh class Engine < ::Rails::Engine isolate_namespace Blorgh end end
Rails.application.routes.draw do mount Blorgh::Engine => "/blorgh" end
$ bundle exec rails generate scaffold article title:string text:text invoke active_record create db/migrate/20170313042107_create_blorgh_articles.rb create app/models/blorgh/article.rb invoke test_unit create test/models/blorgh/article_test.rb create test/fixtures/blorgh/articles.yml invoke resource_route route resources :articles invoke scaffold_controller create app/controllers/blorgh/articles_controller.rb invoke erb create app/views/blorgh/articles create app/views/blorgh/articles/index.html.erb create app/views/blorgh/articles/edit.html.erb create app/views/blorgh/articles/show.html.erb create app/views/blorgh/articles/new.html.erb create app/views/blorgh/articles/_form.html.erb invoke test_unit create test/controllers/blorgh/articles_controller_test.rb invoke helper create app/helpers/blorgh/articles_helper.rb invoke test_unit invoke assets invoke js create app/assets/javascripts/blorgh/articles.js invoke css create app/assets/stylesheets/blorgh/articles.css invoke css create app/assets/stylesheets/scaffold.css
Blorgh::Engine.routes.draw do resources :articles end
bundle exec rake db:migrate
bundle exec rails s
-エンジンルート/config/routes.rbに以下を追加 root to: "articles#index"
$ bundle exec bin/rails generate model Comment article_id:integer text:text invoke active_record create db/migrate/20170313044114_create_blorgh_comments.rb create app/models/blorgh/comment.rb invoke test_unit create test/models/blorgh/comment_test.rb create test/fixtures/blorgh/comments.yml
bundle exec rake db:migrate
<h3>Comments</h3> <%= render @article.comments %>
has_many :comments
<%= render "blorgh/comments/form" %>
<h3>New comment</h3> <%= form_for [@article, @article.comments.build] do |f| %> <p> <%= f.label :text %><br> <%= f.text_area :text %> </p> <%= f.submit %> <% end %>
resources :articles do resources :comments end
$ bundle exec rails g controller comments Expected string default value for '--helper'; got true (boolean) Expected string default value for '--assets'; got true (boolean) create app/controllers/blorgh/comments_controller.rb invoke erb exist app/views/blorgh/comments invoke test_unit create test/controllers/blorgh/comments_controller_test.rb invoke helper create app/helpers/blorgh/comments_helper.rb invoke test_unit invoke assets invoke js create app/assets/javascripts/blorgh/comments.js invoke css create app/assets/stylesheets/blorgh/comments.css
require_dependency "blorgh/application_controller" module Blorgh class CommentsController < ApplicationController def create @article = Article.find(params[:article_id]) @comment = @article.comments.create(comment_params) flash[:notice] = "Comment has been created!" redirect_to articles_path end private def comment_params params.require(:comment).permit(:text) end end end
<%= comment_counter + 1 %>. <%= comment.text %>
bundle exec rails plugin new ~/work/blorgh --mountable --skip-bundle
bundle install --path=vendor/bundle
bundle exec bin/rails generate scaffold article title:string text:text
$ bundle exec bin/rails generate scaffold article title:string text:text invoke active_record create db/migrate/20161025072006_create_blorgh_articles.rb create app/models/blorgh/article.rb invoke test_unit create test/models/blorgh/article_test.rb create test/fixtures/blorgh/articles.yml invoke resource_route route resources :articles invoke scaffold_controller create app/controllers/blorgh/articles_controller.rb invoke erb create app/views/blorgh/articles create app/views/blorgh/articles/index.html.erb create app/views/blorgh/articles/edit.html.erb create app/views/blorgh/articles/show.html.erb create app/views/blorgh/articles/new.html.erb create app/views/blorgh/articles/_form.html.erb invoke test_unit create test/controllers/blorgh/articles_controller_test.rb invoke helper create app/helpers/blorgh/articles_helper.rb invoke test_unit invoke assets invoke js create app/assets/javascripts/blorgh/articles.js invoke css create app/assets/stylesheets/blorgh/articles.css invoke css create app/assets/stylesheets/scaffold.css
bundle exec rake db:migrate
bundle exec rails s
$ bundle exec bin/rails generate model Comment article_id:integer text:text invoke active_record create db/migrate/20161025073042_create_blorgh_comments.rb create app/models/blorgh/comment.rb invoke test_unit create test/models/blorgh/comment_test.rb create test/fixtures/blorgh/comments.yml
bundle exec rake db:migrate