试图让我的 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
控制器:
class UsersController < ApplicationController
def show
@user = User.find_by_username(params[:id])
end
def index
@user = User.find(:all)
end
end
All_Users 视图:
<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