Question

In my rails application I want to accept any string that comes after "users/" link. How do I do it? That is eg. users/xyz or user/asdas is acceptable. How do I do this?


def store_location
    # store last url - this is needed for post-login redirect to whatever the user last visited.
    if (request.fullpath != "/users/sign_in" &&
        request.fullpath != "/users/sign_up" &&
        request.fullpath != "/users/password" &&
        !request.xhr?) # don't store ajax calls
      session[:previous_url] = request.fullpath 
    end
  end

Okay, here is the full story. I have implemented this along with letting user to sign in only if he is approved. The catch is I need to do it for only one type of user so I implemented this in registrations_controller.rb by overriding it.

 def create
    build_resource(sign_up_params)

    if resource.save
      # nil
      if !resource.has_role? :customer
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
  else
    resource.approved=true
    resource.save
    # nil
    set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end

So customer alone can be directly approved but not others. So, when the users get the confirmation email and they click the link they get logged in but get confirmation token invalid error that is probably due to the page getting rerouted back to the confirmation token even on signing in and going to the root path which I have set in the after_sign_in_path. That is why I am trying a work around.

Is this the error or is this something else?

Was it helpful?

Solution

I have solved the problem by using the following code. This make sure when the users clicks the confirmation link he is not redirected back to the confirmation link again on signing in as the confirmable module logs us in after confirmation right away.

I am using Rails 4, Devise version 3.03.

def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
if (!request.fullpath.match("/users/") &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end

OTHER TIPS

This will return the string after "users/" or nil if it does not match:

def getUser(str)
  matched = /users\/(.+)/.match(str)
  if(matched != nil)
    return matched[1]
  else
    # did not match format
  end

  return nil
end

Check out the demo here: http://rubyfiddle.com/riddles/94766

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top