尝试循环 belongs_to 模型

trying to loop through a belongs_to model

正在尝试使用 prawn-rails gem

我知道我做的 posty 方法全错了,因为我没有正确循环它,是吗?我在 posty.pdf.prawn

中的内容也是如此

用户模型

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  first_name :string
#  last_name  :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
require 'elasticsearch/model'

class User < ActiveRecord::Base
    searchkick word_start: [:user]
    has_many :posts
    validates :first_name, :last_name, presence: true
end

Post型号

# == Schema Information
#
# Table name: posts
#
#  id         :integer          not null, primary key
#  title      :string
#  body       :string
#  date       :date
#  user_id    :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Post < ActiveRecord::Base
  belongs_to :user
  validates :title, :body, presence: true
  validates :title, length: { maximum: 140 }
end

路线

  resources :users do 
    resources :posts
  end

根据大虾的指点-rails,我应该在控制器中创建方法,创建一个与方法同名的视图,并填写该视图。

Posts 控制器中的方法

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

posty.pdf.prawn

prawn_document do |pdf|

    pdf.font_size 25 
    pdf.text 'List of Posts by User', :style => :bold

  @post.each do |p|  
    pdf.font_size 16
    pdf.text p.title
  end
end

这是出现的错误:(屏幕截图)

编辑

路线

    users_test GET    /users/test(.:format)                    users#test
   posts_posty GET    /posts/posty(.:format)                   posts#posty
          root GET    /                                        users#index
        search GET    /search(.:format)                        search#search
    user_posts GET    /users/:user_id/posts(.:format)          posts#index
               POST   /users/:user_id/posts(.:format)          posts#create
 new_user_post GET    /users/:user_id/posts/new(.:format)      posts#new
edit_user_post GET    /users/:user_id/posts/:id/edit(.:format) posts#edit
     user_post GET    /users/:user_id/posts/:id(.:format)      posts#show
               PATCH  /users/:user_id/posts/:id(.:format)      posts#update
               PUT    /users/:user_id/posts/:id(.:format)      posts#update
               DELETE /users/:user_id/posts/:id(.:format)      posts#destroy
         users GET    /users(.:format)                         users#index
               POST   /users(.:format)                         users#create
      new_user GET    /users/new(.:format)                     users#new
     edit_user GET    /users/:id/edit(.:format)                users#edit
          user GET    /users/:id(.:format)                     users#show
               PATCH  /users/:id(.:format)                     users#update
               PUT    /users/:id(.:format)                     users#update
               DELETE /users/:id(.:format)                     users#destroy

编辑 2

Post控制器

# == Schema Information
#
# Table name: posts
#
#  id         :integer          not null, primary key
#  title      :string
#  body       :string
#  date       :date
#  user_id    :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class PostsController < ApplicationController

  def posty 
    @posts = User.find(params[:user_id]).posts
  end 

  def new 
    @user = User.find(params[:user_id])
    @post = @user.posts.new
  end

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

  def create 
    @user = User.find(params[:user_id])
    @post = @user.posts.new(post_params)
    if @post.save 
      redirect_to user_path(@post.user)
    else
      render :new
    end
  end

  def update 
    @user = User.find(params[:user_id])
    @post = @user.posts.find(params[:id])
    if @post.update(post_params)
      redirect_to user_path(@post.user)
    else
      render 'edit'
    end
  end


  def destroy
    @user = User.find(params[:user_id])
    @post = @user.posts.find(params[:id])
    @post.destroy
      redirect_to users_path
    end


  private 
  def set_user
     @user = User.find(params[:user_id])
  end

  def set_post
     @post = @user.find(params[:id])
  end

    def post_params
      params.require(:post).permit(:title, :body, :date)
    end
end

用户控制器

# == Schema Information
#
# Table name: users
#
#  id         :integer          not null, primary key
#  first_name :string
#  last_name  :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class UsersController < ApplicationController
  def index
    @users = User.page(params[:page]).per(10)
    query = params[:query]
    query = '*' if query.blank?
    @users = User.search(query, page: params[:page])
  end

  def test 
    @users = User.all
  end

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

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save 
      flash[:notice] = "User successfully added!"
      redirect_to users_path
    else
      render :new
    end
  end

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

  def update
    @user = User.find(params[:id])
    if @user.update(user_params)
      redirect_to users_path
    else
      render :edit
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
      redirect_to users_path
  end

  private 

    def user_params
      params.require(:user).permit(:first_name, :last_name)
    end

end

编辑

再次编辑

我看到的一个问题是,在您的 posty 方法中 @user.posts.find(params[:id]) 期望 @user 实例变量中有一个用户,但在上一行中您分配了 所有 个用户 @user。因此,您会收到该错误消息。

然后在您的 posty.pdf.prawn 文件中 @post 应该是 @posts 以使 each 迭代器工作。所以在你的控制器中你应该 @posts = @user.posts 而不是

Ren 抓得好。 Post belongs_to :userUser has_many :posts 关系适用于单个用户。不是一组用户(User.all 会 return。)

您需要获取单个用户。例如,通过 @user = current_user(如果使用 Devise)。

通过给定的嵌套资源,您有一个 user_id 指向您的用户资源。使用 rake routes 可以深入了解资源路由中约定的参数。因此,在您的 posts#index 操作中,您必须获取用户,然后查询 @posts = User.find(params[:user_id]).posts 之类的帖子。由于您在每个嵌套资源中都需要这个特定用户,因此您可能需要编写一个 before action,例如...

class PostsController < ApplicationController

  before_action :set_user
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # ... the bacon

  private

  def set_user
     @user = User.find(params[:user_id])
  end

  def set_post
     @post = @user.find(params[:id])
  end
end

根据你的关联post belongs_to user,所以应该是一个用户有很多帖子,所以方法应该是,

<%= link_to "Download PDF", posts_posty_path(:format=>:pdf, :user_id => @user.id) %> 

从上面的 link 发送我们将在 posts 方法中使用的 user_id,

  def posty 
    @user = User.find(params[:user_id]) 
    if @user.present?
     @posts = @user.posts
    end
  end 

@post更改为@posts

还在 posty.pdf.prawn 文件中添加一个条件,

prawn_document do |pdf|

    pdf.font_size 25 
    pdf.text 'List of Posts by User', :style => :bold
  if @posts.present?
    @posts.each do |p|  
      pdf.font_size 16
      pdf.text p.title
    end
  end
end

这对你有用。