在我的情况下,即使存在验证错误,它也不会显示验证错误消息。
例如,我将这两列留空,并确保输入了验证码。然后如果我尝试创建新记录。它不会显示验证错误:(它返回表单但没有消息。
我一直在这个项目中使用验证错误消息,我从来没有遇到过这个问题。
任何人都可以在这里找到问题吗?
主题模型中的验证
validates :title,
:presence => {:message => "can't be empty" },
:uniqueness => {:message => "choose unique title" },
:length => { :maximum => 20, :message => "must be less than 20 characters" }
validates :body,
:presence => {:message => "can't be empty" },
:length => { :maximum => 500, :message => "must be less than 20 characters" }
形式
<%= form_for([@community, @topic]) do |f| %>
.....
<%= button_tag( :class => "btn btn-primary") do %>
Create
<% end %>
<% end %>
主题控制器
before_filter :simple_captcha_check, :only => [:update, :create]
def simple_captcha_check
if !simple_captcha_valid?
flash[:error] = 'wrong captcha'
if request.put?
@topic.attributes = params[:topic]
render :action => :edit
elsif request.post?
@topic = Topic.new params[:topic]
render :action => :new
end
end
end
def create
@topic = @community.topics.build (params[:topic])
@topic.user_id = current_user.id
respond_to do |format|
if @topic.save
format.html { redirect_to community_topic_path(@community, @topic), notice: 'Created' }
format.json { render json: [@community, @topic], status: :created, location: @topic }
else
format.html { render action: "new" }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
路线.rb
resources :communities do
resources :topics
end
更新:
视图/布局/application.html.erb
.....
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
</div>
<% end %>
.....