问题
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.
回答1:
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]
.
回答2:
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'
回答3:
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
andshow
(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
来源:https://stackoverflow.com/questions/5411795/public-user-profiles-ruby-on-rails-devise