简体   繁体   English

表单提交失败后,在对话框中显示错误消息

[英]Display error message inside dialog after unsuccessful form submission

I have a login form which pops out inside dialog after user clicks on link. 我有一个登录表单,在用户单击链接后,该表单会在对话框中弹出。 Inside that form I have two username/password fields. 在该表单中,我有两个用户名/密码字段。 If user successfully logs in, it redirects him to his profile page. 如果用户成功登录,它将重定向到他的个人资料页面。 The problem is if user doesn't sign in correctly. 问题是用户是否无法正确登录。 I want to display him some message inside same dialog and not close that dialog. 我想在同一对话框中向他显示​​一些消息,而不是关闭该对话框。

This is my dialog: 这是我的对话框:

<div id="login" >
    <div>
        <%= form_for(:session, url: sessions_path) do |f| %>

        <%= f.label :username %>
        <%= f.text_field :username %>

        <%= f.label :password %>
        <%= f.password_field :password %>

        <%= f.submit "Sign in", class: "btn" %>
        <% end %>
    </div>
</div>

This is my sessions controller: 这是我的会话控制器:

def create
    user = User.find_by(username: params[:session][:username])
        if user && user.authenticate(params[:session][:password])
            sign_in user
            redirect_back_or(user)
        else
            flash.now[:error] = 'Error!'
            render 'new'
        end
end

So, you can see that currently after unsuccessful login, user is redirected to a new form on sign-in page where he can try to sign in again. 因此,您可以看到,当前登录失败后,用户将被重定向到登录页面上的新表单,在那里他可以尝试再次登录。 My rendered error message is normally displayed there: 我呈现的错误消息通常显示在此处:

<%= render 'errors/login', object: f.object %>

If I put that message inside dialog, the following error is displayed immediately after I open site: 如果我将该消息放入对话框中,则在打开网站后立即显示以下错误:

undefined method `errors' for nil:NilClass

Probably because session is still "invisible". 可能是因为会话仍然“不可见”。 If I remove render 'new' for unsuccessful login from SessionsController, I receive the following error: 如果从SessionsController中删除未成功登录的render 'new' ,则会收到以下错误:

Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/Users/username/Applications/AppName/app/views"

So, I only want to keep pop-up dialog window if the user enters wrong username/password and display short message above form. 因此,如果用户输入错误的用户名/密码并在表格上方显示短信,我只想保留弹出的对话框窗口。 What would be the best way to accomplish that? 做到这一点的最佳方法是什么? Thanks :) 谢谢 :)

You've got 2 (3?) issues: 您有2(3?)个问题:


nil:NilClass nil:NilClass

undefined method `errors' for nil:NilClass nil:NilClass的未定义方法“错误”

I would surmise this error is caused by your passing / calling the form object incorrectly 我想这个错误是由您错误地传递/调用表单对象引起的

You're currently passing object: f.object , I would do this: 您当前正在传递object: f.object ,我可以这样做:

<%= render 'errors/login', object: f, as: "form" %>

This will pass the form builder object and call it "form" in the partial. 这将传递表单构建器对象,并在部分视图中将其称为“表单”。 You'd then be able to call the form object like this: 然后,您可以像这样调用表单对象:

#app/views/errors/login.html.erb
<%= form.errors.each do |error| %>
   //errors
<% end %>

Template 模板

Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. 缺少模板会话/创建,使用{:locale => [:en] 、: formats => [:html] 、: handlers => [:erb,:builder,:raw,:ruby,:jbuilder,咖啡]}。 Searched in: * "/Users/username/Applications/AppName/app/views" 在以下位置搜索:*“ /用户/用户名/应用程序/ AppName / app / views”

This means you're calling a file which doesn't exist 这意味着您正在调用一个不存在的文件

Specifically, you'll need to have a file here: 具体来说,您需要在此处拥有一个文件:

#app/views/errors/login.html.erb
<%= your_code_here %>

Unsuccessful 不成功

Causing a dialogue to appear is more of a JS issue than an HTML / Rails one 导致对话出现的问题更多的是JS问题,而不是HTML / Rails问题。

I would certainly look at rendering a .js.erb , as this will allow you to dynamically load the dialogue element on the page 我肯定会看一下渲染一个.js.erb ,因为这将允许您动态加载页面上的对话元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM