我遵循了 rails 3 教程,并试图让它正常工作。
用户发布的所有微博都列在http://localhost:3000/users/username
用户控制器
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate page: params[:page], :per_page => 15
end
每个微博都有一个ID
create_table "microposts", :force => true do |t|
t.text "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "image"
t.text "comment_content"
end
如何设置它,以便http://localhost:3000/users/username/micropost_id
(如果有效)之类的链接将指向只有该微帖子的页面?
我希望显示完全相同,只是在新页面上单独显示。
用户表
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.timestamp "created_at", :null => false
t.timestamp "updated_at", :null => false
t.string "password_digest"
t.string "remember_token"
end
我的配置路线
MyApp::Application.routes.draw do
resources :authentications
resources :microposts, :path => "posts"
root to: 'static_pages#home'
ActiveAdmin.routes(self)
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :microposts do
resources :postcomments
end
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/post', to: 'static_pages#post'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/users/:username/:id', to: 'microposts#show', via: :get, as: :user_micropost
end