Public user profiles? Ruby on Rails + Devise

与世无争的帅哥 提交于 2019-12-03 05:11:06
Danny

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].

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'
malice

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!