문제

I've got a bit of a problem and am looking for your help in finding an original solution.

My rails app has a login form which is inside of a modal window. On successful login, the user is redirected back to the request.referrer. On unsuccessful login, i wish him to be redirected to the same place, but with the login modal open and showing the flash[:error].

I've thought of doing the login with ajax, but I really need a page refresh after a successful login.

My question is: How can I render a page with the login modal window open and showing the error?

I'd appreciate any original ideas! Thanks

도움이 되었습니까?

해결책 2

Big thanks to all replyers. I tried two different solutions inspired by your comments and ended up choosing the second one.

  1. Make the call ajax and then redirect from the rendered js.erb template if login is successfull. Inspired by this answer: https://stackoverflow.com/a/5454887/1936629

  2. Add a url parameter in case of failed login and open the modal window if this parameter exists.

The reason i chose the second option is because it refreshes the page in case of success or failure thus allowing me to use elements like the flash hash or active record error messages.

Thanks again

다른 팁

you can do the following steps -

1) Submit your login form via remote true

 form_for @user, url: your_path remote: true, method: :post

2) In controller do the following things -

 ##if successfully logged in than redirect to root or from where user came (request.referrer)

     return render js: "window.location = '#{request.referrer.present? ? request.referrer : root_path}'" 

##else render to same login page with errors // or put similar logic here

    return render 'login_path' if @user.errors.present?

When you render the page it will not close the modal window

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top