我有 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 %>
但是当发生异常时(比如用户忘记填写字段),我想显示这些异常,而不仅仅是一个警告说“错误”......实际上,我没有找到一种方法来做到这一点
这是我的仪表板控制器
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
任何帮助表示赞赏:)