ruby-on-rails - Ruby on rails Validation 不适用于包含 Action 文本等的 Form 对象
问题描述
错误概述
有一个简单应用程序的 Github URL 重现了底部段落中的问题。
我正在创建一个允许提交文章的应用程序。为了更容易提交文章,我引入了 Rails Action 文本。我还决定添加一个标签功能,所以我在数据库中有一个标签表、一个文章表和一个用户表。文章表单如下图所示。 文章形式
我可以提交一篇文章,但是我想检查无法提交文章的条件的有效性,所以我提交了一个空白字段,但是我收到了以下错误。 无方法错误
我不知道如何解决这个问题,我需要你的帮助。
相关源代码
这是对应部分的视图文件。↓</p>
<%= render "shared/header" %>
<%= form_with model: @article, url: articles_path, class:'form-wrap', local: true do |f| %>
<%= render 'shared/error_messages', model: f.object %>
<div class='article-form'>
<div class="title-field">
<%= f.label :title, "題名" %>
<%= f.text_area :title, class:"input-title" %>
</div>
<div class="tag-field", id='tag-field'>
<%= f.label :name, "タグ" %>
<%= f.text_field :name, class:"input-tag" %>
</div>
<div class="content-field">
<%= f.label :content, "記事本文" %>
<%= f.rich_text_area :content %>
</div>
<div id="search-result">
</div>
</div>
<div class="submit-post">
<%= f.submit "Send", class: "submit-btn" %>
</div>
<% end %>
这是控制器代码。↓</p>
class ArticlesController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
def index
@article = Article.all.order(created_at: :desc)
end
def new
@article = ArticlesTag.new
end
def create
@article = ArticlesTag.new(article_params)
if @article.valid?
@article.save
return redirect_to root_path
else
render :new
end
end
def search
return nil if params[:keyword] == ""
tag = Tag.where(['name LIKE ?', "%#{params[:keyword]}%"] )
render json:{ keyword: tag }
end
private
def article_params
params.require(:articles_tag).permit(:title, :content, :name).merge(user_id: current_user.id)
end
end
这是Article模型的代码。↓</p>
class Article < ApplicationRecord
has_rich_text :content
belongs_to :user
has_one_attached :image
has_many :article_tags
has_many :tags, through: :article_tag_relations
end
这是标签模型的代码。↓</p>
class Tag < ApplicationRecord
has_many :article_tag_relations
has_many :articles, through: :article_tag_relations
validates :name, uniqueness: true
end
这是介于 The Tag 模型和 The Article 模型之间的中间模型。↓</p>
class ArticleTagRelation < ApplicationRecord
belongs_to :article
belongs_to :tag
end
这是收集标签和文章表的 Form 对象类。
class ArticlesTag
include ActiveModel::Model
attr_accessor :title, :name, :content, :user_id
with_options presence: true do
validates :title
validates :name
validates :content
validates :user_id
end
def save
article = Article.create(title: title, content: content, user_id: user_id)
tag = Tag.where(name: name).first_or_initialize
tag.save
ArticleTagRelation.create(article_id: article.id, tag_id: tag.id)
end
end
数据库状态
请帮我。
重现错误的简单应用程序。
github 网址
解决方案
这是你得到的错误
undefined method `body' for "":String
这表示它正在尝试body
在空字符串上调用该方法。
为什么要尝试调用该方法?因为你写了这个:
<%= f.rich_text_area :content %>
所以表单助手期望content
包含一个RichText
(https://api.rubyonrails.org/classes/ActionText/RichText.html)的实例
但是,content
实际上只包含一个 String 实例,因为您的 ArticlesTag 模型没有声明它has_rich_text
(就像您的 Articles 模型一样)
我注意到您的 ArticlesTag 模型没有持久化。我不确定如何将富文本与不持久的模型一起使用——我怀疑这可能是不可能的。
推荐阅读
- blazor - 有没有一种方法可以让递增计数器在索引页面上显示图像,即使是假的,另一个是假的。在身体是我的尝试
- javascript - 绘制后如何下载不同的html画布?
- filter - Mapbox 商店定位器不会加载自定义标记
- java - 如何使用串行接口在 Teensy4.1 和 RaspberryPi 4 之间交换数据
- javascript - 当从文档存储站点检索到的文档在另一个应用程序中打开时,Edge 中会留下空白窗口
- postman - 如何将 Postman 控制台分离到单独的窗口中?
- python - 更改 CreateView Django 的类
- regex - 正则表达式验证 10 位电话号码
- reactjs - 位标记因打字稿错误而失败
- python - 如何找到csv文件中列的平均值?