rich_text_areaを表示させたい
Rails6 のActionTextを導入しました。
原因は、Jsがうまく同期できていないからと思っています。(bootstrap使用)
発生している問題・エラーメッセージ
console
1 2GET http://localhost:3000/posts/css/blog-home.css net::ERR_ABORTED 404 (Not Found) 3new:15 GET http://localhost:3000/posts/vendor/bootstrap/css/bootstrap.min.css net::ERR_ABORTED 404 (Not Found) 4bootstrap.js:9 Uncaught Error: Cannot find module 'popper.js' 5 at webpackMissingModule (bootstrap.js:9) 6 at bootstrap.js:9 7 at Object../node_modules/bootstrap/dist/js/bootstrap.js (bootstrap.js:10) 8 at __webpack_require__ (bootstrap:19) 9 at Module../app/javascript/packs/application.js (application.js:1) 10 at __webpack_require__ (bootstrap:19) 11 at bootstrap:83 12 at bootstrap:83 13
該当のソースコード
formHTMLerb
1<%= form_with(model: post, local: true) do |form| %> 2 <% if post.errors.any? %> 3 <div id="error_explanation"> 4 <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> 5 6 <ul> 7 <% post.errors.full_messages.each do |message| %> 8 <li><%= message %></li> 9 <% end %> 10 </ul> 11 </div> 12 <% end %> 13 14 <div class="field"> 15 <%= form.label :title %> 16 <%= form.text_field :title %> 17 </div> 18 19 <div class="field"> 20 <%= form.label :content %> 21 <%= form.rich_text_area :content %> 22 </div> 23 24 <div class="actions"> 25 <%= form.submit %> 26 </div> 27<% end %> 28
postrb
1 2class Post < ApplicationRecord 3 has_rich_text :content 4end 5
postsController
1class PostsController < ApplicationController 2 before_action :set_post, only: [:show, :edit, :update, :destroy] 3 before_action :move_to_index, except: [:index, :show] 4 before_action :search_post, only: [:index, :search] 5 6 # GET /posts 7 # GET /posts.json 8 def index 9 @posts = Post.includes(:user).order("created_at DESC") 10 end 11 12 # GET /posts/1 13 # GET /posts/1.json 14 def show 15 end 16 17 # GET /posts/new 18 def new 19 @post = Post.new 20 end 21 22 # GET /posts/1/edit 23 def edit 24 end 25 26 # POST /posts 27 # POST /posts.json 28 def create 29 @post = Post.new(post_params) 30 31 respond_to do |format| 32 if @post.save 33 format.html { redirect_to @post, notice: 'Post was successfully created.' } 34 format.json { render :show, status: :created, location: @post } 35 else 36 format.html { render :new } 37 format.json { render json: @post.errors, status: :unprocessable_entity } 38 end 39 end 40 end 41 42 # PATCH/PUT /posts/1 43 # PATCH/PUT /posts/1.json 44 def update 45 respond_to do |format| 46 if @post.update(post_params) 47 format.html { redirect_to @post, notice: 'Post was successfully updated.' } 48 format.json { render :show, status: :ok, location: @post } 49 else 50 format.html { render :edit } 51 format.json { render json: @post.errors, status: :unprocessable_entity } 52 end 53 end 54 end 55 56 # DELETE /posts/1 57 # DELETE /posts/1.json 58 def destroy 59 @post.destroy 60 respond_to do |format| 61 format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 62 format.json { head :no_content } 63 end 64 end 65 66 def search 67 @posts = @p.result.includes(:category) # 検索条件にマッチした商品の情報を取得 68 end 69 70 private 71 # Use callbacks to share common setup or constraints between actions. 72 def set_post 73 @post = Post.find(params[:id]) 74 end 75 76 # Only allow a list of trusted parameters through. 77 def post_params 78 params.require(:post).permit(:title, :content) 79 end 80 81 def move_to_index 82 unless user_signed_in? 83 redirect_to action: :index 84 end 85 end 86 87 def search_post 88 @p = Post.ransack(params[:q]) # 検索オブジェクトを生成 89 end 90end 91
ConfigWebpackEnvironmentJs
1const { environment } = require('@rails/webpacker') 2const { VueLoaderPlugin } = require('vue-loader') 3const vue = require('./loaders/vue') 4 5environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin()) 6environment.loaders.prepend('vue', vue) 7 8 9environment.config.resolve.alias = { 'vue$': 'vue/dist/vue.esm.js' } 10 11 12const webpack = require('webpack') 13environment.plugins.prepend( 14 'Provide', 15 new webpack.ProvidePlugin({ 16 $: 'jquery', 17 jQuery: 'jquery', 18 Popper: 'popper.js' 19 }) 20) 21 22module.exports = environment
applicationJs
1// This file is automatically compiled by Webpack, along with any other files 2// present in this directory. You're encouraged to place your actual application logic in 3// a relevant structure within app/javascript and only use these pack files to reference 4// that code so it'll be compiled. 5 6require("@rails/ujs").start() 7// require("turbolinks").start() 8require("@rails/activestorage").start() 9require("channels") 10require('./preview') 11require.context('../images', true) 12 13// Uncomment to copy all static images under ../images to the output folder and reference 14// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 15// or the `imagePath` JavaScript helper below. 16// 17// const images = require.context('../images', true) 18// const imagePath = (name) => images(name, true) 19 20require("trix") 21require("@rails/actiontext") 22 23import 'bootstrap'; 24import '../stylesheets/application';
JavascriptStylesheetsApplicationSCSS
1@import '~bootstrap/scss/bootstrap'; 2@import '~trix/dist/trix'; 3
applicationHTMLewrb
1<!-- Bootstrap core CSS --> 2 <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> 3 4 <!-- Custom styles for this template --> 5 <link href="css/blog-home.css" rel="stylesheet"> 6 7 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 8 <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> 9 <%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %> 10 11 </head>
試したこと
Rails 6にjQueryとBootstrapを導入。
Webpackerで入れるようにしました。
ブログ機能を表示させたいです。
どうかご教授お願いいたします。
あなたの回答
tips
プレビュー