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
bundle exec rails new ~/work/unicorn --skip-bundle
gem 'devise' gem 'blorgh', path: "/Users/sora/work/blorgh"
bundle install --path=vendor/bundle
mount Blorgh::Engine, at: "/blog"
$ bundle exec rake railties:install:migrations Copied migration 20170313063027_create_blorgh_articles.blorgh.rb from blorgh Copied migration 20170313063028_create_blorgh_comments.blorgh.rb from blorgh
bundle exec rake db:migrate
bundle exec rails s
bundle exec rails g model user name:string bundle exec rake db:migrate
<div class="field"> <%= f.label :author_name %><br> <%= f.text_field :author_name %> </div>
def article_params params.require(:article).permit(:title, :text, :author_name) end
attr_accessor :author_name belongs_to :author, class_name: "User" before_validation :set_author private def set_author self.author = User.find_or_create_by(name: author_name) end
$ bin/rails g migration add_author_id_to_blorgh_articles author_id:integer
$ bundle exec rake blorgh:install:migrations $ bundle exec rake db:migrate
<p> <b>Author:</b> <%= @article.author %> </p>
def to_s name end