私は2つのコントローラーを持っています:DocumentsController
そしてDashboardController
ユーザーログインが成功すると、彼はにリダイレクトされますdashboard_path
。これには、このような「高速ドキュメント」を作成するためのフォームがあります。
<%= form_for @document, :html => {:class => 'well'} do |f| %>
<% if @document.errors.any? %>
<div id="alert alert-block">
<div class="alert alert-error">
<h2>Couldn't create your doc. :(</h2>
<ul>
<% @document.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<label>A lot of fields</label>
<%= f.text_field :fields %>
<div class="form-actions">
<%= f.submit 'Create document', :class => 'btn btn-large' %>
</div>
<% end %>
しかし、例外が発生した場合(ユーザーがフィールドに入力するのを忘れた場合など)、「エラー」というアラートだけでなく、これらの例外を表示したいと思います...実際、これを行う方法が見つかりませんでした
これが私のDashboarControllerです
class DashboardController < ApplicationController
before_filter :authenticate
def index
@document = Document.new
end
end
と私のDocumentsController
class DocumentsController < ApplicationController
respond_to :json, :html
def show
end
def create
@document = Document.new(params[:document])
@document.user = current_user
if @document.save
redirect_to dashboard_path, notice: 'Created!'
else
flash[:error] = 'Error!'
redirect_to dashboard_path
end
end
end
どんな助けでもありがたいです:)