控制器之间的交互
在我的网络应用程序上,我有一个登录页面,位于页面控制器中,名为welcome.html.erb,
其中有一个简单的登录表单和一个简单的注册表单,两者都在同一页面上。
如果用户使用错误的凭据注册(例如密码确认错误或密码长度等),则处理此问题的控制器是用户控制器中的新方法。
在新方法中,它检查是否创建了用户,如果没有,我希望它返回到页面控制器中的welcome方法,将用户创建期间创建的错误传递给它。
看来,如果我执行
redirect_to root_path
返回根页面时,注册的错误计数将被重置。如果我调用用户控制器的其他操作,我确实会看到错误。 但由于所有 html 代码都在欢迎视图中,我不想也在用户控制器视图中复制代码。
有没有办法将错误数据传递到页面控制器?
错误的部分是
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
我所说的使用
<%= render 'shared/error_messages', :object => f.object %>
On my web app I have a login page which is in the Pages Controller called welcome.html.erb
Inside it has a simple form for login and a simple for for sign up, both on the same page.
If the user signs up with wrong credentials (like password confirmation wrong, or length of password and etc) the controller that handles this is the new method in the Users Controller.
Inside the new method it checks if a user is created, and if not I'd like it to return to the welcome method in Pages Controller, passing to it the errors that were created during the user's creation.
It seems that if I do a
redirect_to root_path
The error count of the signup is reset when returning to the root page. If instead I call some other action of the users controller I do see the errors.
But since all of the html code is in the welcome view I don't want to replicate the code in the users controller views as well..
Is there a way to pass that errors data to the pages controller?
The partial for the error is
<% if object.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2>
<p>There were problems with the following fields:</p>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Which I call using
<%= render 'shared/error_messages', :object => f.object %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(或者无论实际路径是什么。)
(Or whatever the actual path is.)
从用户控制器中,我调用了
flash[:errors][email protected]
然后我将部分更改为
我所说的使用
现在解决了问题..
From the Users controller I called
flash[:errors][email protected]
and then I changed the partial to be
Which I called using
That solved the issue for now..