Ruby-on-rails 使用 Devise 创建一个“用户”显示页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7086583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a `Users` show page using Devise
提问by steffi2392
I'm trying to create a User showpage (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 showdefinition - is there any way I can access the controllers Devise is implementing in order to make one or do I have to override them?
我正在尝试创建一个用户show页面(将用作个人资料页面),但对如何使用 Devise 执行此操作感到困惑。似乎 Devise 没有任何类型的show定义 - 有什么方法可以访问 Devise 正在实施的控制器以制作控制器还是我必须覆盖它们?
回答by Sergey Kishenin
You should generate a users_controllerwhich inherits from application_controllerand define there your custom showmethod. Don't forget to create a view and routes for it.
Ex:
您应该生成一个users_controller继承自application_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]
回答by Ashitaka
Don't forget that your users routes should be below the devise_for users routes, like this:
不要忘记你的用户路由应该低于 devise_for users 路由,像这样:
#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:
此外,如果您使用用户名或电子邮件作为主键而不是通常的 id,您应该通过如下声明您的路由来避免路由冲突:
#routes.rb
devise_for :users, :path_prefix => 'd'
resources :users, :only => [:show]
回答by Manny Quintanilla
showing current_user/ other_user profiles with devise:
使用设备显示 current_user/other_user 配置文件:
After installing devise
安装设备后
Create a Users controller:
创建一个用户控制器:
rails generate controller Users
Then create a show action and find the user with params id:
然后创建一个 show 动作并找到具有 params id 的用户:
def show
@user = User.find(params[:id])
end
Create a show.html.erb file in the User view folder:
在用户视图文件夹中创建一个 show.html.erb 文件:
<%= @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:
在用户视图文件夹中创建一个 index.html.erb 然后:
<% @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:
这会将您链接到用户 index.html.erb 文件,在那里您将创建一个循环并链接到用户配置文件:
<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>
This should work!
这应该有效!
回答by Kleber S.
you can generate the views used by devise, so you can change it as you want to.
您可以生成设计使用的视图,因此您可以根据需要进行更改。
rails g devise:views

