【发布时间】:2021-06-15 10:17:42
【问题描述】:
在我的 Rails 应用程序中创建模型的新实例时,当字段未通过验证时,我试图显示错误消息。出于某种原因,这些错误从未像应有的那样真正显示在网站上的字段旁边。 但是,错误出现在 Chrome DevTools 的“网络”选项卡的“预览”部分中。因此错误正在正确生成。在终端中,它说new.html.erb 已渲染,但我认为它实际上并没有?任何帮助将不胜感激 - 我在网上没有找到太多关于此的信息。如果有帮助,我正在使用 Tailwind CSS 来设置前端样式。
这是我的代码:
occasion.rb
class Occasion < ApplicationRecord
belongs_to :user
has_many :registries
has_many :charities, :through => :registries
validates :occasion_name, presence: true
validates :occasion_date, presence: true
validates :url, presence: true, format: { without: /\s/ }, uniqueness: true
validates_uniqueness_of :user_id
end
occasions_controller.rb
class OccasionsController < ApplicationController
load_and_authorize_resource only: [:new, :create, :edit, :update, :destroy]
def index
@occasions = Occasion.all
end
def show
@occasion = Occasion.where(url: params[:url]).first
end
def new
@occasion = Occasion.new
end
def create
@occasion = Occasion.new(occasion_params)
@occasion.user = current_user
if @occasion.save
respond_to do |format|
format.html { redirect_to new_registry_path }
format.js { render :js => "window.location='#{ new_registry_path }'" }
end
else
render :new
end
end
def edit
@occasion = Occasion.find(params[:id])
end
def update
@occasion = Occasion.find(params[:id])
if @occasion.update(occasion_params)
redirect_to @occasion
return
else
render :edit
end
end
def destroy
@occasion = Occasion.find(params[:id])
@occasion.destroy
redirect_to occasions_path
end
private
def occasion_params
params.require(:occasion).permit(:user_id, :occasion_name, :occasion_date, :url)
end
# user authentication is not required for show
skip_before_action :authenticate_user!, :only => [:show]
end
new.html.erb
<%= form_with model: @occasion do |form| %>
<div class="text-center">
<%= form.label :occasion_name, "Occasion Name", class: "text-red-400 font-semibold px-8" %><br>
<%= form.text_field :occasion_name, class: "rounded w-2/5" %>
<% @occasion.errors.full_messages_for(:occasion_name).each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<div class="text-center py-2">
<%= form.label :occasion_date, "Occasion Date", class: "text-red-400 font-semibold px-8" %><br>
<%= form.date_field :occasion_date, type: "date", class: "rounded" %>
<% @occasion.errors.full_messages_for(:occasion_date).each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<div class="text-center py-2">
<%= form.label :url, 'URL', class: "text-red-400 font-semibold px-8" %><br>
<%= form.text_field :url, class: "rounded" %>
<% @occasion.errors.full_messages_for(:url).each do |message| %>
<div><%= message %></div>
<% end %>
<em><div class="text-sm">domainname.com/yourURLhere</div></em>
</div>
<div class="text-center py-2">
<%= form.submit occasion.persisted? ? 'Update' : 'Save', class: "rounded-full bg-red-400 text-white px-3" %>
</div>
<% end %>
【问题讨论】:
-
看起来表单是作为 AJAX 请求提交的。能否检查一下项目中是否设置了
config.action_view.form_with_generates_remote_forms = true? -
@ArunKumarMohan 这将在 Rails 项目中的哪个文件中?我在我的项目中的任何地方都没有看到它。我是 Rails 的新手——来自 Django。谢谢!
-
应该添加到
config/application.rb。无论如何,看起来它没有添加到项目配置中。这是不必要的,但请尝试将local: true添加到form_with调用 (form_with model: @occasion, local: true)。你能分享表单提交请求的服务器日志吗? -
@ArunKumarMohan 解决了!谢谢!!!如果您想将此作为答案发布,我会接受。
标签: ruby-on-rails error-handling google-chrome-devtools