带有设计的用户个人资料页面 - 显示操作的路由 [英] User profile pages with devise - Routing to show action

查看:21
本文介绍了带有设计的用户个人资料页面 - 显示操作的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用设备和单独的控制器(用户)进行身份验证,这是一个显示"操作

Authentication with devise and a separate controller (Users) that is a single 'show' action

class UsersController < ApplicationController
#before_filter :authenticate_user!

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

    respond_to do |format|
        format.html # show.html.erb
        format.xml { render :xml => @user }
    end
  end
end

目前视图(users/show.html.erb)只显示访问的配置文件的用户名

So far the view (users/show.html.erb) only displays the username of the accessed profile

<%= @user.username %>

路由也:

resources :users

我希望这些个人资料页面可以公开访问,如果他们获得指向它的链接/告诉地址,但也希望当前登录的用户可以访问他的个人资料的链接.我的标题片段:

I would like these profile pages to be publically accessible if they get a link to it/told the address, but also want a link available to a currently logged in user to visit his profile. My a snippet from my header:

<li><%= link_to "Profile", @user %></li>
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: "delete" %></li>

目前,@user 只是为了阻止路由错误.我不完全确定要链接到什么,已经尝试了很多组合,但显然我的 Rails 新手头脑缺少一些我需要做的额外事情.非常感谢任何帮助!

Currently, @user is just there to stop a routing error. I'm not entirely sure what to link to, have tried quite a few combinations but obviously my rails newbie mind is missing something extra I need to do. Any help is much appreciated!

(Rails 4,红宝石 2.0.0)

(Rails 4, ruby 2.0.0)

附注我还希望最终允许指向个人资料页面的链接以以下格式显示 id + 用户名:#{id}-#{username}(而不是仅仅是 users/1 --> users/1-bbvoncrumb).

Side note I would also like to, eventually, allow the link to a profile page to display the id + username in the format: #{id}-#{username} (instead of just being users/1 --> users/1-bbvoncrumb).

推荐答案

如果你想让它链接到,你只需要将 User 实例作为参数传递给 link_to给定用户的 show 页面.因此,如果您想在 Devise 中链接到当前登录用户的个人资料,您只需要:

You just need to pass User instance as a parameter to link_to if you want it to link to given user's show page. So if you want to link to currently logged in user's profile in Devise, you just need:

<li><%= link_to "Profile", current_user %></li>

这篇关于带有设计的用户个人资料页面 - 显示操作的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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