我在rails 7.0.4和顺风上使用ruby 3.0.2。
我有一个简单的模型,有一个简单的验证,一般的验证行为正在按预期工作,即在控制台和浏览器中测试时错误显示,表单没有按预期提交-所以我知道验证在幕后工作。如果我填写了这个字段,表单就会提交。
问题是没有在表单上触发验证错误。操作错误块可以确认文本将以指定的方式出现。我想知道这是否与涡轮机没有更新屏幕有关?:
型号:
class Account < ApplicationRecord
has_many :users
validates :name, presence: true
end表格:
<% if @account.errors.any? %>
<div class="text-red-700">
<h4>
<%= "#{pluralize(@account.errors.count, "error")} prohibited this account from being saved:" %>
</h4>
<ul>
<% @account.errors.full_messages.each do |msg| %>%
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label :name, class: 'block mb-2 text-sm font-medium text-gray-900' %>
<%= f.text_field :name, placeholder: 'Enter your Account\'s Name', class: 'bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5' %>
</div>
<%=f.submit "Save", class: "text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center" %>
<% end %>```
Thanks in advance!发布于 2022-10-10 08:19:52
噢,我看我没有把控制器包括在问题里,这就是问题所在。我没有想到我在盲目地读一本(付费的)电子书。在检查我以前的一个项目中的控制器时,我发现控制器中的操作在render部分中没有呈现的状态。在我修好它之后应该是这样的
def create
@account = Account.new(account_params)
if @account.save
current_user.account = @account
current_user.add_role :admin, @account
current_user.save
redirect_to root_path, success: "Your account has been created!"
else
render :new, status: :unprocessable_entity
end
endhttps://stackoverflow.com/questions/74008386
复制相似问题