首页 > 解决方案 > 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 网址

重现错误的简单应用程序

标签: ruby-on-railsruby

解决方案


这是你得到的错误

undefined method `body' for "":String

这表示它正在尝试body在空字符串上调用该方法。

为什么要尝试调用该方法?因为你写了这个: <%= f.rich_text_area :content %> 所以表单助手期望content包含一个RichTexthttps://api.rubyonrails.org/classes/ActionText/RichText.html)的实例

但是,content实际上只包含一个 String 实例,因为您的 ArticlesTag 模型没有声明它has_rich_text(就像您的 Articles 模型一样)

我注意到您的 ArticlesTag 模型没有持久化。我不确定如何将富文本与不持久的模型一起使用——我怀疑这可能是不可能的。


推荐阅读