控制器之间的交互

发布于 2024-12-12 14:16:20 字数 1110 浏览 2 评论 0原文

在我的网络应用程序上,我有一个登录页面,位于页面控制器中,名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-12-19 14:16:20
redirect_to welcome_path

(或者无论实际路径是什么。)

redirect_to welcome_path

(Or whatever the actual path is.)

辞旧 2024-12-19 14:16:20

从用户控制器中,我调用了

flash[:errors][email protected]
然后我将部分更改为

<% if object.any? %>

    <div id="error_explanation">
        <h2><%= pluralize(object.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.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

我所说的使用

<%= render 'shared/error_messages', :object =>@user_errors %>

现在解决了问题..

From the Users controller I called

flash[:errors][email protected]
and then I changed the partial to be

<% if object.any? %>

    <div id="error_explanation">
        <h2><%= pluralize(object.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.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

Which I called using

<%= render 'shared/error_messages', :object =>@user_errors %>

That solved the issue for now..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文