【发布时间】:2018-09-21 21:43:50
【问题描述】:
我遇到的问题可能很容易解决,虽然我做了很多搜索并没有找到解决方案。
我的 _errors.html.erb 中有
<% if obj.errors.any? %>
<div class="row">
<div class="col-md-8 col-md-offset-2 col-xs-12">
<div class="panel panel-danger">
<div class="panel-heading">
<h2 class="panel-title">
<%= pluralize(obj.errors.count, "error") %>
prohibided this form from being saved:
</h2>
<div class="panel-body">
<ul>
<% obj.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
</div>
<% end %>
然后我的 edit.html.erb 中有一个表单,并且:
<%= render 'layouts/errors', obj: @my_obj_here %>
然后在控制器中更新/创建(让我们以更新为例):
def update
if @my_obj_here.update(params[:my_obj_here].permit(:body))
redirect_to my_path_here_path(@my_obj_here), notice: "Something."
else
render 'edit'
end
end
当我尝试更新并且提交信息无效时出现问题,这将属于“渲染'编辑'”
错误显示正确(在这种情况下最大长度为 100),但我的网址更改为: my_obj_here/1/edit 到 my_obj_here/1
这不应该发生。
接下来我尝试用"redirect_to :back" 替换“render 'edit'”,但这只是忽略了
<%= render 'layouts/errors', obj: @my_obj_here %> 在edit.html.erb中。
谁能帮我弄清楚如何渲染相同的my_obj_here/1/edit? 我相信我需要使用“渲染”我的方法,因为重定向只会跳过部分错误。
另外,在我的更新方法中,您可能注意到了“如果为真”:
redirect_to my_path_here_path(@my_obj_here), notice: "Something."
我也可以这样做,只需将我的代码更改为:
redirect_to :back, :notice => "something."
这会起作用,但不会像我希望它们在使用我的错误部分时显示的那样显示错误。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2