更改用户参数以包含他们的用户名 [英] Changing user params to include their username

查看:36
本文介绍了更改用户参数以包含他们的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在我的应用中查看用户页面,您必须输入他们的 ID /user/2.

To view a user page on my app you have to enter their id /user/2.

我怎样才能让它在参数中使用他们的 username 而不是用户显示页面的 id 值?我希望它是 /user/username/username.

How can I make it so that it uses their username in the params instead of id value for user show page? I would like it to be /user/username or /username.

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

路线:

  get 'signup' => 'users#new'
  get 'login' => 'sessions#new'
  get 'logout' => 'sessions#destroy'
  get 'edit' => 'users#edit'
  get "/profile/:id" => "users#show"
  get "profile/:id/settings" => 'users#edit'
  get 'settings/:id' => 'users#settings'

 resources :users do
    resources :messages do
      post :new
      collection do
        get :askout
      end
    end
    collection do
        get :trashbin
        post :empty_trash
     end
  end

用户控制器:

  def show
    @user = User.find(params[:id])    
  end

推荐答案

根据我的经验,我发现最简单的方法是使用 friendly_id 宝石.ActiveRecord 中有一个 to_param 方法,您可以设置它来定义模型的路由 id 将是什么,但是如果您要使用的属性不是 URL 友好的,它将变得非常复杂.假设您的用户名不包含空格或其他 URL不友好"字符,您可以执行以下操作:

In my experience the easiest way I've found to do this is to use the friendly_id gem. There is a to_param method in ActiveRecord that you can set to define what a model's route id is going to be, but if the attribute you want to use is not already URL friendly it will become very complicated. Assuming your usernames contain no spaces or other URL "unfriendly" characters, you could do something like this:

class User < ActiveRecord::Base
  ...
  def to_param
    username
  end
  ...
end

但请确保在您的控制器中您可以通过用户名找到用户.

But make sure in your controller you then find users by their username.

class UsersController < ApplicationController
  ...
  def set_user
    @user = User.find_by(username: params[:id])
  end
end

请注意,如果 to_param 中的值与数据库中的值不完全相同,则再次查找对象将更加困难.例如,如果您想使用 name.parameterize 来设置像 /users/john-doe 这样的 URL,而您的实际名称属性是 John Doe>,您必须找到一种方法来始终如一地取消参数化"您的参数.或者,您可以创建一个新的数据库列,其中包含 url 中所需属性的唯一参数化字符串(称为 slug).这就是我使用 friendly_id 的原因.它以半自动方式处理您的 slug.

Note that if the value in to_param is not EXACTLY what the value in the database is, finding your object again is going to be more difficult. For example, if you wanted to use name.parameterize to set have URLs like /users/john-doe when your actual name attribute is John Doe, you'll have to find a way to consistently "deparameterize" your parameter. Or, you can create a new database column that contains a unique parameterized string of the attribute you want in the url (called a slug). That's why I use friendly_id. It handles your slugs semi-automatically.

至于将您的用户路由到 /username,您有多种选择:

As for routing your users to /username, you have several options:

  • get ':id', to: 'users#show', as: 'show'
  • 资源'用户',路径:'/'

只要确保将这些路由放在路由文件的末尾即可.这样,如果您尝试通过转到 /some_other_modelSomeOtherModelsController 上获得 index 操作,它不会尝试为您找到具有用户名some_other_model".

Just make sure you put these routes at the end of your routes file. That way if you try to get to your index action on SomeOtherModelsController by going to /some_other_model it's not going to try to find you a user with username "some_other_model".

这篇关于更改用户参数以包含他们的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆