0

私の Rails アプリケーションでは、「users/」リンクの後に続く文字列を受け入れたいと考えています。どうすればいいのですか?それは例えばです。users/xyzまたはuser/asdas許容されます。どうすればいいですか?


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

さて、これが完全な話です。ユーザーが承認された場合にのみサインインできるようにするとともに、これを実装しました。問題は、1 種類のユーザーに対してのみ実行する必要があるため、これをregistrations_controller.rbオーバーライドして実装しました。

 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

そのため、顧客のみを直接承認できますが、他の顧客は承認できません。そのため、ユーザーが確認メールを受け取り、リンクをクリックすると、ログインしますが、おそらくサインインしてルートパスに移動しても、ページが確認トークンに再ルーティングされるため、確認トークン無効エラーが発生しますに設定していafter_sign_in_pathます。それが私が回避策を試みている理由です。

これはエラーですか、それとも別のことですか?

4

2 に答える 2

1

次のコードを使用して問題を解決しました。これにより、ユーザーが確認リンクをクリックしたときに、確認可能なモジュールが確認後にすぐにログインするため、サインイン時に再度確認リンクにリダイレクトされないようにします。

Rails 4、Devise バージョン 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
于 2013-10-14T10:17:30.470 に答える
0

これは、「users/」の後の文字列を返します。一致しない場合は nil を返します。

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

  return nil
end

ここでデモをチェックしてください: http://rubyfiddle.com/riddles/94766

于 2013-10-13T21:44:24.890 に答える