我正在使用帖子和用户在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,: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
解决方法
这是Ruby on Rails的相当直接的使用.我建议阅读
Active Record Basics以加快速度.
首先,你应该在帖子和用户之间有一个belongs_to关系,如下所示:
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end
这会向User实例添加.posts方法,为Post实例添加.user方法.
然后,您必须决定如何使应用程序的URL结构起作用.以下是我头脑中的几个选项:
> / posts?user =:user_id
> / posts / by /:user_id
> / users /:id / posts
鉴于用户和他们的帖子之间的关系,我的推荐(我相信一般的“Rails方式”)将是#3.所以,让我们添加路由到config / routes.rb:
创建JUST路由的简短方法:
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 %>
这应该为您提供显示用户帖子的基本功能.您可以通过重复使用部分或其他一些漂亮的快捷方式来增强它,但我会将其作为练习让您弄清楚.