Ruby on Rails-宝石级-显示#<User:0x007fd8d5424108>而不是用户名

哈塞尔登(J. Haselden)

我正在创建一个在Ruby on Rails上运行的应用程序,并使用其社区组件使用了成熟的Gem:https : //github.com/thredded/thredded

目前,在应用程序的老部分中,它正在显示

#<User:0x007fd8d5424108>

它应该显示用户名的位置。

我在User上有一个:name列,用于定义其用户名。我也在使用Devise进行注册。

显示用户名时的链接如下所示:<%=链接到thredded_current_user,user_path(thredded_current_user)%>

现在我知道我可以将thredded_current_user更改为thredded_current_user.name,但是这需要找到在thredded gem中使用的所有位置,将其复制到我的应用程序,然后更改视图。我在想必须有一个更简单的答案,我只是不知道什么。

除了将thredded_current_user的所有实例更改为thredded_current_user.name之外,还有另一种显示用户名的方法吗?

这是繁琐的初始化程序的代码:

# frozen_string_literal: true
# Thredded configuration

# ==> User Configuration
# The name of the class your app uses for your users.
# By default the engine will use 'User' but if you have another name
# for your user class - change it here.
Thredded.user_class = 'User'

# User name column, used in @mention syntax and should be unique.
# This is the column used to search for users' names if/when someone is @ mentioned.
Thredded.user_name_column = :name

# The path (or URL) you will use to link to your users' profiles.
# When linking to a user, Thredded will use this lambda to spit out
# the path or url to your user. This lambda is evaluated in the view context.
Thredded.user_path = lambda do |user|
  user_path = :"#{Thredded.user_class.name.underscore}_path"
  main_app.respond_to?(user_path) ? main_app.send(user_path, user) : "#{user.to_param}"
end

# This method is used by Thredded controllers and views to fetch the currently signed-in user
Thredded.current_user_method = :"current_#{Thredded.user_class.name.underscore}"

# User avatar URL. rb-gravatar gem is used by default:
Thredded.avatar_url = ->(user) { user.profile_picture.url }

# ==> Permissions Configuration
# By default, thredded uses a simple permission model, where all the users can post to all message boards,
# and admins and moderators are determined by a flag on the users table.

# The name of the moderator flag column on the users table.
Thredded.moderator_column = :admin
# The name of the admin flag column on the users table.
Thredded.admin_column = :admin

# This model can be customized further by overriding a handful of methods on the User model.
# For more information, see app/models/thredded/user_extender.rb.

# ==> Email Configuration
# Email "From:" field will use the following
# Thredded.email_from = '[email protected]'

# Incoming email will be directed to this host
# Thredded.email_incoming_host = 'example.com'

# Emails going out will prefix the "Subject:" with the following string
# Thredded.email_outgoing_prefix = '[My Forum] '

# Reply to field for email notifications
# Thredded.email_reply_to = -> postable { "#{postable.hash_id}@#{Thredded.email_incoming_host}" }

# ==> View Configuration
# Set the layout for rendering the thredded views.
Thredded.layout = 'thredded/application'

# ==> Error Handling
# By defau

lt Thredded just renders a flash alert on errors such as Topic not found, or Login required.
# Below is an example of overriding the default behavior on LoginRequired:
#
# Rails.application.config.to_prepare do
#   Thredded::ApplicationController.module_eval do
#     rescue_from Thredded::Errors::LoginRequired do |exception|
#       @message = exception.message
#       render template: 'sessions/new', status: :forbidden
#     end
#   end
# end

这是我的路线代码:

Rails.application.routes.draw do
  get 'page/home'
  get 'page/feed'
  get 'page/about'
  get 'page/contact'

  mount Thredded::Engine => '/community'

  devise_for :users, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }, :controllers  => {
             :registrations => 'users/registrations',  :omniauth_callbacks => "users/omniauth_callbacks" }

  resources :users,  path: "", only: [:show] do
    resources :tracks
  end

  resources :tracks, only: [:create, :destroy]

  resources :feeds, only: [:create, :destroy, :show]
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
root 'page#home'



  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

end
瑞安·克里斯平·汉尼斯(Ryan Crispin Heneise)

这实际上与带螺纹的宝石无关。视图正在调用to_s用户对象。如果放在<?= thredded_current_user =>视图中,ERB将自动调用to_sthredded_current_user对象。link_to辅助方法中也会发生同样的事情

您可以to_s像这样在User模型中覆盖该方法:

class User < ActiveRecord::Base
  def to_s
    name
  end
  ...
end

另外,您可以为该name方法添加别名,使其to_s像这样:

alias_method :to_s, :name

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 CakePHP 中显示用户名而不是 user_id

来自分类Dev

Ruby on Rails - 在新页面/控制器上显示用户名

来自分类Dev

Rails管理员显示用户名,而不是用户#5

来自分类Dev

如何在Ruby on Rails上设置用户名验证?

来自分类Dev

使用Ruby on Rails Devise Gem向用户名注册

来自分类Dev

松弛-显示用户的全名而不是用户名

来自分类Dev

用户评论显示数字而不是用户名

来自分类Dev

ps命令显示用户标识而不是用户名

来自分类Dev

用户名显示为root而不是登录用户?

来自分类Dev

用外键显示用户名而不是用户ID

来自分类Dev

如何使用 xlwt 显示用户名而不是用户 ID

来自分类Dev

显示用户名?

来自分类Dev

Ruby on Rails / Ruby:用文件中的用户名填充数据库

来自分类Dev

为什么/ user / {用户名}不会自动显示?

来自分类Dev

Rails:将用户ID保存到数据库,但仅在视图中显示用户名

来自分类Dev

如何在RUBY中显示当前用户名作为链接?

来自分类Dev

如何在RUBY中显示当前用户名作为链接?

来自分类Dev

Ruby on Rails:用户名/密码错误?(535身份验证失败)

来自分类Dev

ruby on rails 匿名化已删除的记录用户名

来自分类Dev

rails,friendly_id,设计用户名不显示showing

来自分类Dev

无法显示用户名

来自分类Dev

显示海报的用户名

来自分类Dev

无法显示用户名

来自分类Dev

Rails Ruby Geometry宝石量产

来自分类Dev

Ruby on Rails的优质OAuth宝石

来自分类Dev

显示当前用户的用户名(Swift)

来自分类Dev

红宝石级的动态特性

来自分类Dev

什么比红宝石级少

来自分类Dev

Ruby on Rails:“显示”帐户路由