私の状況では、検証エラーがあったとしても、どういうわけか検証エラーメッセージが表示されません。
たとえば、これらの 2 つの列を空のままにして、キャプチャ ワードが入力されていることを確認します。次に、新しいレコードを作成しようとするとします。検証エラーは表示されません:(フォームに戻りますが、メッセージは表示されません。
このプロジェクトでは常に検証エラー メッセージを使用してきましたが、この問題に直面したことはありません。
誰でもここで問題を見つけることができますか?
トピック モデルでの検証
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 %>
.....