###環境
OS:Ubuntu server 20.04STL
Ruby:2.7.2
Rails:6.0.3.4
###状況
このブログを参考にリッチテキストを作成したところ、postしたものが表示されませんでした。postはできて、showで見れない感じです。titleは表示されます。よろしくお願いします。
###コンソールログ
console
1rails new blog 2cd blog 3bundle install 4bundle exec rails action_text:install 5bundle exec rails g scaffold post title:string 6bundle exec rails db:create 7bundle exec rails db:migrate 8rails s 9 10 11Rendered posts/show.html.erb within layouts/applicationEverything's up-to-date. Nothing to doStarted GET "/posts/7/edit" for 192.168.0.7 at 2020-11-16 18:49:05 +0900 12Cannot render console from 192.168.0.7! Allowed networks: 127.0.0.0/127.255.255.255, ::1 13Processing by PostsController#edit as HTML 14 Parameters: {"id"=>"7"} 15 Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 7], ["LIMIT", 1]] 16 ? app/controllers/posts_controller.rb:72:in `set_post' 17 Rendering posts/edit.html.erb within layouts/application 18 ActionText::RichText Load (0.1ms) SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = ? AND "action_text_rich_texts"."record_type" = ? AND "action_text_rich_texts"."name" = ? LIMIT ? [["record_id", 7], ["record_type", "Post"], ["name", "body"], ["LIMIT", 1]] 19 ? app/views/posts/_form.html.erb:18 20 Rendered posts/_form.html.erb (Duration: 7.1ms | Allocations: 2367) 21 Rendered posts/edit.html.erb within layouts/application (Duration: 8.6ms | Allocations: 2739) 22[Webpacker] Everything's up-to-date. Nothing to do 23Completed 200 OK in 22ms (Views: 18.0ms | ActiveRecord: 0.3ms | Allocations: 7289)
###コード
#####blog/app/models/post
class Post < ApplicationRecord has_rich_text :body end
#####blog/app/controllers/posts_controller.rb
class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def post_params params.require(:post).permit(:title, :body) end # GET /posts # GET /posts.json def index @posts = Post.all end # GET /posts/1 # GET /posts/1.json def show end # GET /posts/new def new @post = Post.new end # GET /posts/1/edit def edit end # POST /posts # POST /posts.json def create @post = Post.new(post_params) respond_to do |format| if @post.save format.html { redirect_to @post, notice: 'Post was successfully created.' } format.json { render :show, status: :created, location: @post } else format.html { render :new } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # PATCH/PUT /posts/1 # PATCH/PUT /posts/1.json def update respond_to do |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'Post was successfully updated.' } format.json { render :show, status: :ok, location: @post } else format.html { render :edit } format.json { render json: @post.errors, status: :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.json def destroy @post.destroy respond_to do |format| format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_post @post = Post.find(params[:id]) end # Only allow a list of trusted parameters through. def post_params params.require(:post).permit(:title) end end
#####blog/app/views/posts/_form.html.erb
<%= 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 %> <%= form.label :body %> <%= form.rich_text_area :body %> </div> <div class="actions"> <%= form.submit %> </div> <% end %>
#####blog/app/views/posts/show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>Title:</strong> <%= @post.title %> </p> <%= @post.body %> <%= link_to 'Edit', edit_post_path(@post) %> | <%= link_to 'Back', posts_path %>
あなたの回答
tips
プレビュー