我是Ruby on Rails的新手,我遇到的问题是没有在表单上显示错误消息。我已经被困在这几天了,我已经尝试了几乎所有我已经看到的这个错误的解决方案/建议,但我无法使它工作。我需要表格,一个用户和一个绿色。它们都使用相同的共享消息部分,并且用户表单在输入无效时显示错误消息,但绿色表单不显示。
绿色表格(以绿色计量视图显示):
<%=form_for(@green) do |f| %>
<%= render partial: 'shared/error_messages', object: f.object %>
<div class="form-group">
<div class="col-xs-2"><legend> Add new metrics:</legend></div>
<div class="col-xs-2 ">
<%= f.number_field :number, class: 'form-control', placeholder: "Green number" %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.number_field :speed, class: 'form-control', placeholder: "Green speed", :step => 'any' %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.number_field :firmness, class: 'form-control', placeholder: "Green firmness", :step => 'any' %>
</div>
</div>
<div class="form-group">
<div class="col-xs-2 ">
<%= f.date_field :measure_date, class: 'form-control', placeholder: "Date of measurement" %>
</div>
</div
<div class="form-group">
<div class="col-xs-1 ">
<%= f.submit "Submit", class: "btn btn-primary" %>
</div>
</div
</div>
<% end %>
用户表单:
<% provide(:title, 'Register') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :first_name %>
<%= f.text_field :firstname, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :lastname, class: 'form-control' %>
<%= f.label :golf_course %>
<%= f.text_field :golf_course, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :username %>
<%= f.text_field :username, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Password Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
共享消息部分
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
绿色模特
class Green < ActiveRecord::Base
belongs_to :user
default_scope -> { order(measure_date: :desc, number: :asc) }
validates :user_id, presence: true
validates :number, presence: true
validates :speed, presence: true
validates :firmness, presence: true
validates :measure_date, presence: true
end
用户模型:
class User < ActiveRecord::Base
has_many :greens, dependent: :destroy
before_save { self.username = username.downcase}
before_save { self.email = email.downcase}
validates :firstname, presence: true, length: { maximum: 50 }
validates :lastname, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
#,uniqueness: { case_sensitive: false}
validates :golf_course, presence: true, length: { maximum: 50 }
validates :username, presence: true, length: { maximum: 25 },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, length: {minimum: 6}
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
Controller
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@greens = @user.greens
end
def greenmetrics
@user = current_user
@greens = @user.greens
@green = current_user.greens.build if logged_in?
end
格林斯控制器:
def create
@user = current_user
@green = current_user.greens.build(green_params)
if @green.save
flash[:success] = "Green metrics saved!"
@greens = @user.greens
redirect_to (:back)
else
flash[:danger] = "Please verify that all data is valid"
redirect_to (:back)
end
end
我不得不添加闪存危险,因为验证的错误消息没有显示...
提前感谢您的帮助。
安德烈
答案 0 :(得分:1)
新绿色永远不会有任何错误,因为它尚未经过验证。
答案 1 :(得分:0)
我弄清楚出了什么问题,我错过了每个字段的定义:'form-control'。一旦我将其添加到表单并在保存不成功时重新呈现,就会显示错误消息。
更新表格:
<%=form_for(@green, :url => greenmetrics_path, :html => {:method => :post}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<section>
<div class ="col-xs-2"><legend> Metrics for: </legend> </div>
<div class ="col-xs-2"><%= f.date_field :measure_date, value: @date %></div>
</section>
<section>
<div class="form-group">
<div class="col-xs-2"><legend> Add metrics:</legend></div>
<div class="col-xs-2 "> <%= f.number_field :number, placeholder: "Green number", class: 'form-control' %> </div>
<div class="col-xs-2 "> <%= f.number_field :speed, placeholder: "Green speed", class: 'form-control', :step => 'any' %> </div>
<div class="col-xs-2 "> <%= f.number_field :firmness, placeholder: "Green firmness", class: 'form-control', :step => 'any' %> </div>
<div class="col-xs-2 "> <%= f.number_field :moisture, placeholder: "Green moisture", class: 'form-control', :step => 'any' %> </div>
<div class= "col-xs-1"> <%= f.submit "Add", class: "btn btn-primary" %> </div>
</div>
</section>
<% end %>
更新了控制器:
def create
@user = current_user
@green = current_user.greens.build(green_params)
if @green.save
flash[:success] = "Green metrics saved!"
@greens = @user.greens
@measure_date = green_params[:measure_date]
redirect_to greenmetrics_path(:date => @measure_date)
else
render 'greenmetrics'
#flash[:danger] = "Please verify that all data is valid"
#redirect_to (:back)
end
end