You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 8 ActionText上传图片失败,出现block in create如何调试?

Rails 8 ActionText 图片无法保存问题排查

我正在探索Rails 8的ActionText功能,使用Windows 11搭配Ubuntu WSL环境。按照RoR官网教程搭建基础博客后遇到问题:富文本内容能保存,但图片无法保存,不确定是Ubuntu权限问题还是其他原因。

尝试上传dummyapp/dummy_files目录下的本地图片时,控制台出现block in create提示,排查无果。试过在Rails控制台输出@post.errors.messages,但未显示任何错误。

相关代码

post.rb

class Post < ApplicationRecord
  has_rich_text :body
end

posts_controller.rb

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, status: :unprocessable_entity }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
    
 
private
    def set_post
      @post = Post.find(params.expect(:id))
    end

    def post_params
      params.expect(post: [ :title, :body ])
    end

posts/_form.rb

<%= form_with(model: post) do |form| %>
  <% if post.errors.any? %>
    <div style="color: red">
    <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
        <% post.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div>
    <%= form.label :title, style: "display: block" %>
    <%= form.text_field :title %>
  </div>

  <div>
    <%= form.label :body, style: "display: block" %>
    <%= form.rich_textarea :body %>
  </div>

  <div>
    <%= form.submit %>
  </div>
<% end %>

控制台输出

Started POST "/posts" for ::1 at 2024-11-28 19:23:07 -0800
Processing by PostsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"title"=>"Suck an egg!", "body"=>"<div></div>"}, "commit"=>"Create Post"}
  TRANSACTION (0.2ms)  BEGIN /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  Post Create (1.3ms)  INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES ('Suck an egg!', NULL, '2024-11-29 03:23:07.667889', '2024-11-29 03:23:07.667889') RETURNING "id" /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  ActionText::RichText Create (0.4ms)  INSERT INTO "action_text_rich_texts" ("name", "body", "record_type", "record_id", "created_at", "updated_at") VALUES ('body', '<div></div>', 'Post', 17, '2024-11-29 03:23:07.691089', '2024-11-29 03:23:07.691089') RETURNING "id" /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  ActiveStorage::Attachment Load (0.3ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = 16 AND "active_storage_attachments"."record_type" = 'ActionText::RichText' AND "active_storage_attachments"."name" = 'embeds' /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  Post Update (0.3ms)  UPDATE "posts" SET "updated_at" = '2024-11-29 03:23:07.697759' WHERE "posts"."id" = 17 /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  TRANSACTION (2.5ms)  COMMIT /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
Redirected to http://localhost:3000/posts/17
Completed 302 Found in 53ms (ActiveRecord: 4.9ms (4 queries, 0 cached) | GC: 1.0ms)

问题原因及解决方案

从控制台输出能看到,提交的body参数只有<div></div>,说明图片根本没被提交到后端,结合代码和环境,问题主要出在这几个地方:

  1. 控制器参数写法错误
    你用了params.expect(post: [ :title, :body ]),这是语法错误,正确的强参数写法应该是:

    def post_params
      params.require(:post).permit(:title, :body)
    end
    

    另外set_post里的params.expect(:id)也要改成params.require(:id),错误的参数写法会导致ActionText无法正确接收图片相关的上传参数。

  2. ActiveStorage 未配置
    ActionText依赖ActiveStorage处理图片附件,先确认是否执行过以下命令:

    rails active_storage:install
    rails db:migrate
    

    这会创建ActiveStorage相关的数据库表,没有的话图片无法存储。

  3. WSL 目录权限问题
    检查项目下的storage和tmp/storage目录权限,执行命令调整权限:

    chmod -R 755 storage/ tmp/
    

    WSL下Windows和Linux的权限映射可能导致Rails进程无法读写存储目录。

  4. 前端JS依赖未加载
    确保app/javascript/application.js里引入了ActiveStorage和ActionText的JS:

    import "@rails/activestorage"
    import "@rails/actiontext"
    

    没有这些JS,ActionText的图片上传功能无法正常触发。


内容的提问来源于stack exchange,提问作者Ramona

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 00:42:01