使用 Devise 创建“Users”显示页面

发布于 2024-11-29 16:29:51 字数 164 浏览 1 评论 0原文

我正在尝试创建一个用户 show 页面(它将用作个人资料页面),但对如何使用 Devise 执行此操作感到困惑。 Devise 似乎没有附带任何类型的 show 定义 - 有什么方法可以访问 Devise 正在实现的控制器以便制作一个控制器,还是我必须覆盖它们?

I'm trying to create a User show page (that will function as a profile page) but am confused about how to do this with Devise. It doesn't seem as though Devise comes with any sort of show definition - is there any way I can access the controllers Devise is implementing in order to make one or do I have to override them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰葑 2024-12-06 16:29:51

您应该生成一个继承自 application_controllerusers_controller 并在其中定义您的自定义 show 方法。不要忘记为其创建视图和路线。
前任:

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

#in your view
<%= @user.name %>

#routes.rb
match 'users/:id' => 'users#show', via: :get
# or 
get 'users/:id' => 'users#show'
# or
resources :users, only: [:show]

You should generate a users_controller which inherits from application_controller and define there your custom show method. Don't forget to create a view and routes for it.
Ex:

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

#in your view
<%= @user.name %>

#routes.rb
match 'users/:id' => 'users#show', via: :get
# or 
get 'users/:id' => 'users#show'
# or
resources :users, only: [:show]
时光病人 2024-12-06 16:29:51

不要忘记您的用户路由应位于 devise_for users 路由下方,如下所示:

#routes.rb
devise_for :users
resources :users, :only => [:show]

此外,如果您使用用户名或电子邮件作为主键而不是通常的 id,则应通过将路由声明为来避免路由冲突跟随:

#routes.rb
devise_for :users, :path_prefix => 'd'
resources :users, :only => [:show]

Don't forget that your users routes should be below the devise_for users routes, like this:

#routes.rb
devise_for :users
resources :users, :only => [:show]

Also, if you are using a username or an email as the primary key instead of the usual id, you should avoid routing conflicts by declaring your routes as follow:

#routes.rb
devise_for :users, :path_prefix => 'd'
resources :users, :only => [:show]
噩梦成真你也成魔 2024-12-06 16:29:51

使用设备显示当前用户/其他用户配置文件:

安装设备后

创建一个 Users 控制器:

rails generate controller Users

然后创建一个 show 操作并找到带有 params id 的用户:

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

在 User view 文件夹中创建一个 show.html.erb 文件:

<%= @user.email %>

链接到用户显示页面:

<%= link_to "current_user_show", current_user %>

现在,如果您想查看其他配置文件,请创建用户控制器中的索引操作:

def index @users = User.all end

在用户视图文件夹中创建一个index.html.erb,然后:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

该链接将是:

<%= link_to "show_index_of_users", users_path %>

这将链接到用户index.html.erb文件,您将在其中创建一个循环和链接至用户个人资料:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

这应该可行!

showing current_user/ other_user profiles with devise:

After installing devise

Create a Users controller:

rails generate controller Users

Then create a show action and find the user with params id:

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

Create a show.html.erb file in the User view folder:

<%= @user.email %>

Linking to users show page:

<%= link_to "current_user_show", current_user %>

Now if you want to view other profiles create a index action in the users controller:

def index @users = User.all end

Create an index.html.erb in the User view folder then:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

The link for this will be:

<%= link_to "show_index_of_users", users_path %>

This will link you to the users index.html.erb file there you will create a loop and link to users profile:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

This should work!

窗影残 2024-12-06 16:29:51

users_controller.rb 应位于 devise 生成的 users 文件夹之外,其中包含 sessionsregistration< /code> 控制器(如果有)

# controllers/users_controller.rb
class  UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

对于您的视图:

# views/users/show.html.erb
<h1><%= @user.some_user_attribute_here %></h1>

以及您的路线:

# config/routes.rb
  get '/users/:id', to: 'users#show'

show 路线也应该与自定义 devise 生成的路线分开(如果有的话)。

The users_controller.rb should be outside of the devise generated users folder containing the sessions and registration controllers (if any).

# controllers/users_controller.rb
class  UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

For your views:

# views/users/show.html.erb
<h1><%= @user.some_user_attribute_here %></h1>

and for your routes:

# config/routes.rb
  get '/users/:id', to: 'users#show'

The show route should also be separate from the custom devise generated ones (if any).

输什么也不输骨气 2024-12-06 16:29:51

您可以生成 devise 使用的视图,因此您可以根据需要更改它。

 rails g devise:views

you can generate the views used by devise, so you can change it as you want to.

 rails g devise:views
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文