2013-07-28 49 views
6

我正在使用帖子和用户在Rails上创建自己的博客。当我点击他时,我需要显示来自特定作者的所有帖子(这里的概念是:link)。我应该怎么做? 请说什么额外的信息或代码我要补充如何从特定用户获取所有帖子

users_controller:

class UsersController < ApplicationController 
def show 
@user = User.find(params[:id]) 
@posts = @user.posts 
    end 

end 

posts_controller:

class PostsController < ApplicationController 
before_filter :authenticate_user!, :except => [:show, :index] 

# GET /posts 
# GET /posts.json 



def index 
@posts = Post.all 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @posts } 
    end 
end 

# GET /posts/1 
# GET /posts/1.json 
def show 
@post = Post.find(params[:id]) 

respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @post } 
    end 
end 

# GET /posts/new 
# GET /posts/new.json 
def new 
@post = Post.new 

    respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @post } 
    end 
end 

# GET /posts/1/edit 
def edit 
    @post = Post.find(params[:id]) 
end 

# POST /posts 
# POST /posts.json 
def create 
#@post = Post.new(params[:post]) 
@post = current_user.posts.build(params[:post]) 
respond_to do |format| 
    if @post.save 
    format.html { redirect_to @post, notice: 'Post was successfully created.' } 
    format.json { render json: @post, status: :created, location: @post } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @post.errors, status: :unprocessable_entity } 
    end 
    end 
end 

# PUT /posts/1 
# PUT /posts/1.json 
def update 
@post = Post.find(params[:id]) 

respond_to do |format| 
    if @post.update_attributes(params[:post]) 
    format.html { redirect_to @post, notice: 'Post was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @post.errors, status: :unprocessable_entity } 
    end 
    end 
end 

# DELETE /posts/1 
# DELETE /posts/1.json 
def destroy 
@post = Post.find(params[:id]) 
@post.destroy 

respond_to do |format| 
    format.html { redirect_to posts_url } 
    format.json { head :no_content } 
    end 
end 
end 

用户模型:

class User < ActiveRecord::Base 
# Include default devise modules. Others available are: 
# :token_authenticatable, :confirmable, 
# :lockable, :timeoutable and :omniauthable 
has_many :posts, :dependent => :destroy 
validates :fullname,  :presence => true, :uniqueness => true 
validates :password,  :presence => true 
validates :email,   :presence => true, :uniqueness => true 


devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

attr_accessible :email, :password, :password_confirmation, :fullname 


end 

桩模型:

class Post < ActiveRecord::Base 
attr_accessible :text, :title 

validates :user_id, :presence => true 
validates :title, :presence => true 
validates :text, :presence => true 

belongs_to :user 
has_many :comments 
end 
+0

您还需要控制器中的其他操作以及正确的迁移和路由。看看http://stackoverflow.com/questions/9597229/activerecordrecordnotfound-couldnt-find-user-without-an-id – Berlin

回答

13

这是on Rails的一个相当简单的使用Ruby。我建议阅读Active Record Basics以加快速度。

首先,你应该有一个看起来像这样的帖子和用户之间的belongs_to的关系:

class User < ActiveRecord::Base 
    has_many :posts 
end 

class Post < ActiveRecord::Base 
    belongs_to :user 
end 

这增加了.posts方法将用户实例.user方法将邮实例

然后,您必须决定您希望应用程序的URL结构如何工作。下面是从我的头顶几个选项:

  1. /posts用户=:USER_ID
  2. /职位/的/:user_id说明
  3. /用户/:ID /职位

鉴于用户和他们的帖子之间的关系,我的建议(我相信一般的“Rails方式”)将是#3。所以,让我们的路由添加到config/routes.rb

短的方式来创建只是路线:

get 'users/:id/posts' => 'users#posts', :as => :user_posts 

长的方式来创建基于resources路线:

resources :users do 
    member do 
    get :posts 
    end 
end 

这两种方法都将提供名为user_posts_path的帮助程序方法和一个称为user_posts_url的帮助程序方法,可以在您的视图中使用该程序链接到使用link_to的用户帖子列表helper方法:

<%= link_to post.user.name, user_posts_path(post.user) %> 

现在,你必须添加控制器行动app/controllers/users_controller.rb

class UsersController < ActionController::Base 

    def posts 
    @user = User.find(params[:id]) 
    @posts = @user.posts 
    end 

end 

,然后添加你的HTML/ERB代码app/views/users/posts.html.erb

<% @posts.each do |post| %> 
    <%= post.inspect %> 
<% end %> 

这应该给你显示用户帖子的基本能力。您可以通过重新使用后部分或其他一些不错的快捷方式来增强它,但我会将其作为一个练习让你去弄清楚。

+0

非常感谢,它工作正常! – user2596615

+1

我已经设置了使用“get'users /:id/posts'=>'users#posts',:as =>:user_posts”的路由,但是我仍然没有路由匹配[GET]“/ users/1 /帖子“错误。任何想法可能是错的? – mirap

1

您需要2个型号:用户和帖子。他们之间有一个关系:用户有很多帖子,向用户发布BELONGS。要在数据库中创建这个关系,您应该将user_id列添加到posts表中。要做到这一点,只需运行以下命令:

rails generate migration AddUserIdToPosts user_id: integer 

不要忘了运行耙分贝:迁移后

要建立协会之间的模型添加到用户模式:

has_many :posts, dependent: :destroy 

并发布模型:

belongs_to :user 

现在您可以使用'用户'方法上post和'posts'方法用户。例如,在用户控制器的表演动作:

@user = User.find(params[:id]) 
@posts = @user.posts 

这个链接将帮助您: http://guides.rubyonrails.org/association_basics.html http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

+0

非常感谢您的回答,但我做了你说的一个错误发生后:ActiveRecord :: UsersController中的RecordNotFound#show 找不到ID的用户 – user2596615

+0

向您显示UsersController – Berlin

相关问题