SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member ancientnight's Avatar
    Join Date
    Mar 2008
    Location
    Seattle
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't create a redirect

    So I've been stuck on something that should be very simple for the last couple of hours. I've created an Ajax login system in Rails but I have no idea how to redirect the user to a new controller action upon authentication. I've tried (redirect_to :controller => "update"), but that doesn't bring me to a new page, it just inserts that content within the current view.

    Please help... I feel so stupid!!!
    -Steven

  2. #2
    SitePoint Member
    Join Date
    Oct 2003
    Location
    York, UK
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You say you're trying to redirect them to a different action, but the code you included shows you redirecting them to a controller... If you're trying to redirect the user to the 'update' action in the same controller as that which you are redirecting them from, then you'll need:

    Code:
    redirect_to :action => 'update'
    If that's not what you were after then you'll have to post some code for us

  3. #3
    SitePoint Member ancientnight's Avatar
    Join Date
    Mar 2008
    Location
    Seattle
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *I'm not sure how to indent my code but luckily it is not that complicated. *

    Sorry, I am trying to redirect to a controller. The problem I am having is that my login authentication uses Ajax. It returns a descriptive string based on the login credentials. That is “User name does not exist”, “Bad password”, “Enter a password” etc… If the login and password are valid it sets a session variable and returns back to the index controller. If I try to use redirect_to :controller => “update” within the Ajax call (after valid authentication is established) it just inserts the corresponding “update view” html into the current page rather than redirect to the new page.

    Hope this makes sense. :)

    My Controller Code…

    def index
    if session[:id]
    redirect_to :controller => "update"
    end
    end

    def login
    if params[:email] == "" and params[:password] == ""
    render :text => "Please enter email & password"
    elsif params[:email] == ""
    render :text => "Please enter email"
    elsif params[:password] == ""
    render :text => "Please enter password"
    elsif
    message = User.hello(params[:email], params[:password])
    if message == "Welcome"
    session[:id] = params[:email]
    #this is what I want to do:
    #redirect_to :controller => “update”

    render :text => "Welcome"
    else
    render :text => message
    end
    end
    end
    end


    So I'm left with a page that validated the user but didn't go to a new page.
    The form tag I'm using in my view is:

    <% form_remote_tag(:update => "login_box_middle", :url => { :action => "login" }, :loading => "status('login_box_middle')", :complete => "reload()", :position => "top" ) do %>

    My solution to the problem so far:

    :complete calls a callback function called "reload()". This callback runs after the Ajax request and looks at the content returned by that request. If the request returns "Welcome" then we know the user is valid and we refresh the page in which the index action will now redirect us to the correct "user controller" because a session variable has been set.

    function reload(){
    if (login_box_middle.innerHTML == "Welcome"){
    location.reload(true);
    }
    }


    This is sloppy but the only thing I could think of (does not work on Macs).

    Here is the page in question.

    http://www.stevenhgoodwin.com/cnt_mgt/public/user

    Type in something and try to login. You will get some kind of message returned by an Ajax call.

    Login Credentials:
    email: test
    pass: test1

    I'd like to be able to redirect_to the "update" controller within the Ajax call and just simply refresh the page after the request.

    Thanks for any help! I know this is not great code but I'm having fun with it and I hope to keep growing! :)
    -Steven

  4. #4
    SitePoint Member
    Join Date
    Oct 2003
    Location
    York, UK
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whenever I've used render :text => 'foo' in a controller, it's only ever to find and fix bugs.

    When I use it I've noticed that the code stops executing right there. Try taking that out render :text => "Welcome" and only having the redirect. That should work.

    You won't lose anything in terms of welcoming them to the site, seeing as though you can set the welcome message in the Flash and show it when they've been redirected...

    Give that a go and let me know if that works. It also looks like you have one too many 'ends' at the bottom of that code, but that might be down to having to edit it for this post.

  5. #5
    SitePoint Member ancientnight's Avatar
    Join Date
    Mar 2008
    Location
    Seattle
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally the render :text => “Welcome” did not exist. I only use it now because the redirect was not working. The problem is that the redirect works, but it returns the corresponding view page as the ajax result. It actually doesn’t go to the new page.

    I guess what I’m looking for is a way for the server to terminate the Ajax request and then perform the redirect. Would that work? Is there a way to do that?

    And the extra "end" thing might have just been a cutting/pasting error on my part.
    -Steven


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •