简体   繁体   中英

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:

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:


nil:NilClass

undefined method `errors' for 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:

<%= 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]}. Searched in: * "/Users/username/Applications/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

I would certainly look at rendering a .js.erb , as this will allow you to dynamically load the dialogue element on the page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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