尝试使我的 all_users 页面呈现拥有帐户的所有用户的列表 - 然后每个列表项将是指向该用户的个人资料页面的链接。我的页面正确呈现用户名,但它们没有指向其用户页面。他们的用户页面应该是/user/username,但是链接指向/username,这显然会产生错误。我真的非常非常感谢您的帮助。 Stack Overflow 确实令人难以置信。
型号:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
has_many :items
validates_presence_of :username
validates_uniqueness_of :username
def to_param
username
end
end
Controller :
class UsersController < ApplicationController
def show
@user = User.find_by_username(params[:id])
end
def index
@user = User.find(:all)
end
end
所有用户查看:
<div class="well">
<h1>All users</h1>
<% @user.each do |user| %>
<%= link_to user.username, public_profile_path(user.username) %><br/>
<% end %>
</div>
路线:
Scratch::Application.routes.draw do
resources :items
devise_for :users
match 'users/:id' => 'users#show'
match '/users', :to => 'users#index', :as => "all_users", :via => "get"
match ':username' => 'users#show', via: :get, as: :public_profile
root :to => 'static_pages#home'
get "about" => "static_pages#about"
end
最佳答案
match '/username/:name' => 'users#show', via: :get, as: :public_profile
关于html - 从 All_Users 页面链接到用户配置文件、路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057959/