Public user profiles? Ruby on Rails + Devise

后端 未结 3 1926
悲&欢浪女
悲&欢浪女 2021-02-06 19:40

How to make public user profiles? I use Devise and only have /user/. User can see only his profile, I want to make /user/user_id.

user_id - public user profile.

3条回答
  •  温柔的废话
    2021-02-06 20:04

    To make a public profile using devise, make sure you have a route set up like

    resources :users
    

    and make sure you have a controller action set up to handle the show resource. If not, create a controller users_controller.rb and give it a class definition, along with a show method.

    class UsersController < ApplicationController
      def index
        @users = User.all
    
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @users }
        end
      end
    
      def show
        @user = User.find(params[:id])
    
        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @user }
        end
      end
    end
    

    Next browse to /users/1 or /users/2 (if you have two users set up) and you should see both profiles. To set them to private, you should do a before filter in the controller action, checking the current_user id against the param[:user_id].

提交回复
热议问题