Ruby on rails 如何强制用户完成整个表单?
我正在创造一个歌唱形式。我需要用户填写表单的每个字段,但是rails只在没有密码的情况下阻止创建。我必须做些什么来强制用户在提交前完成整个表单 这是我的表格代码:Ruby on rails 如何强制用户完成整个表单?,ruby-on-rails,ruby,html,Ruby On Rails,Ruby,Html,我正在创造一个歌唱形式。我需要用户填写表单的每个字段,但是rails只在没有密码的情况下阻止创建。我必须做些什么来强制用户在提交前完成整个表单 这是我的表格代码: <%= form_for(user) do |f| %> <% if user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(user.errors.count, "error"
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :fav_team, "Favorite Team" %>
<%= collection_select(:user, :fav_team, League.order(:name), :id, :name, {:include_blank => "Select a League"}, { :id => "leagues_select"}) %>
<%= grouped_collection_select(:user, :fav_team, League.order(:name), :teams, :name, :id, :name, {:include_blank => true}, {}) %>
</div>
<div class="field">
<%= f.label :net_worth, "Net Worth (USD)" %>
<%= f.text_field :net_worth, :readonly => true, :value => "100" %>
</div>
<div class="field">
<%= f.label :country %>
<%= f.select :country, options_for_Countrys, :include_blank => true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
您必须在模型中编写验证,以确保数据有效且存在 在你的情况下,你必须这样做
class User < ApplicationRecord
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
end
通过谷歌搜索客户端验证开始。OP要求客户端验证,而不是服务器端验证,也就是说,强制用户在提交前完成整个表单。