Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

ruby on rails - Validation error display is producing this 'undefined method `full_message' for :category:Symbol'

I'm following the Rails Guide to implement this, but I'm getting this error, and I'm not entirely sure how to fix it.

Error display in form:

    <% if @group.errors.any? %>
        <div id="error_exp">
            <h2><%= pluralize(@group.errors.count, "error") %> prohibited this article from being saved:</h2>
            <ul>
                <% @group.errors.each do |error| %>
                    <li><%= error.full_message %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

Controller create action: (I'm testing the error display on a new group form)

    def create
        @group = current_user.groups.build(group_params)

        if @group.save
            redirect_to user_groups_path(current_user)
        else
            render :new
        end
    end

Model validations:

class Group < ApplicationRecord
    has_many :cards, dependent: :destroy
    belongs_to :category
    belongs_to :user
end

class Category < ApplicationRecord
    has_many :groups
    has_many :users, through: :groups

    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false
    
end

class Card < ApplicationRecord
    belongs_to :group

    validates :front,:back, presence: true
end

Thank you for the help!

question from:https://stackoverflow.com/questions/65830021/validation-error-display-is-producing-this-undefined-method-full-message-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Rails guide is for 6.1. Prior to that errors.each yielded |attribute,error| Rails 6.0 and Rails 6.1

I am assuming you are below 6.1 based on your error in which case you want to use:

<% @group.full_messages.each do |message| %>
  <li><%= message %></li>
<% end %> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...