[英]Rails - Validation Error Messages is not Appearing
I'm having problems with validation error messages in my Rails4 application. 我在Rails4应用程序中遇到验证错误消息的问题。 They don't show up somehow. 他们不会以某种方式出现。 When they triggered, only the "Please review the problems below" message is appearing and nothing more, no error messages. 当他们触发时,仅显示“请检查以下问题”消息,仅此而已,没有错误消息。
order.rb => order.rb =>
Class Order < ActiveRecord::Base
....
attr_accessor :card_number, :card_verification
validate :validate_card, :on => :create
private
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
errors.add(:base, message)
end
end
end
end
orders_controller.rb => orders_controller.rb =>
def create
@order = Order.new(order_params)
@order.ip_address = request.remote_ip
@order.user_id = current_user.id
respond_to do |format|
if @order.save
if @order.purchase
format.html { render action: "success", notice: 'Perfect!' }
else
format.html { render action: "failure" }
end
else
format.html { render action: 'new' }
end
end
end
/orders/new.html.rb => /orders/new.html.rb =>
<%= simple_form_for(@order, html:{class: "well"}, :method => :post) do |f| %>
<%= f.error_notification %>
<%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: lambda{|x| x.examination.name}, label: 'Choose' %>
<%= f.input :first_name, label: 'Name' %>
<%= f.input :last_name, label: 'Surname' %>
.........
<%= f.button :submit %>
<% end %>
What is wrong with my code? 我的代码有什么问题?
Thanks in advance. 提前致谢。
Try this>>> 试试这个>>>
Class Order < ActiveRecord::Base
....
attr_accessor :card_number, :card_verification
validate :validate_card, :on => :create
private
def validate_card
unless credit_card.valid?
errors.add(:credit_card, "your message")
end
end
end
You can try this. 你可以试试看 This will generate one error at a time 一次将产生一个错误
<% if @order.errors.full_messages.any? %>
<% @order.errors.full_messages.each do |error_message| %>
<%= error_message if @order.errors.full_messages.first == error_message %> <br />
<% end %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.