Public user profiles? Ruby on Rails + Devise

后端 未结 3 1923
悲&欢浪女
悲&欢浪女 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:00

    I'm not that experienced, but I did it like this:

    • In controllers create a users_controller.rb

    • In views create /users/ typical views, for example index and show (of course define them in your controller)

    • In routes add resources :users

    You can simply add migrations to the users table, like

    $ rails g migration add_birthday_to_users birthday:date
    

    and show this on the users/:id page

    0 讨论(0)
  • 2021-02-06 20:02

    You should create a route on config/routes.rb file like below. And then implement your controller method and view.

      match 'show/:id' => 'user#show'
    
    0 讨论(0)
  • 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].

    0 讨论(0)
提交回复
热议问题