【问题标题】:how to add account name in the url and redirect or get the records based on account name如何在url中添加账户名并根据账户名重定向或获取记录
【发布时间】:2019-06-01 08:18:57
【问题描述】:

例如:- http://localhost:3000/account_name/dashboard 所有用户都将拥有一个基于我需要的帐户名的帐户名 在 account_name 前面附加所有要附加的 URL,并且需要基于该帐户名称的所有记录

基本上,GitHub 将如何重定向到我们的存储库,就像我需要实现的那样。 例如:- https://github.com/account_name/ecommerce

我不知道我必须在谷歌中搜索什么

我遇到了基于约束的重定向,但我不确定。

关联:- 用户与帐户有一个关联。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案1】:

    您可以在您的会话或登录控制器中尝试这样的操作。如果用户正在登录您的应用程序。

    def create
        @user = User.find_by(email: params[:email]) #you can try your own sign_in method
         if @user && @user.valid_password?(params[:password])
          redirect_to get_redir_link(@user) #you can call the redirect link here
        else
          redirect_to login_path
        end
    end
    
    
    def get_redir_link(user)
        account_name = user.account_name #by using user find the account name for your scenarios
        return "#{account_name}/dashboard" #return the url whatever you want
    end
    

    在我的应用程序中,我做了类似的事情。你可以利用它。

    【讨论】:

    • 但是如果我像这样附加 account_name 可能会出现路由错误。尽管还有许多其他链接,但我需要以相同的方式进行操作。
    • 请在我们可以做到的基础上分享用户和帐户名的关联。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2021-06-26
    • 1970-01-01
    • 2022-10-16
    • 2020-12-08
    相关资源
    最近更新 更多