Ruby Question

trying to loop through a belongs_to model

Trying to use a

gem.

I know that that I'm doing the
posty
method all wrong because I'm not looping through it correctly, am I? Same goes for what I have in
posty.pdf.prawn


User Model

# == 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 Model

# == 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


routes

resources :users do
resources :posts
end


According to directions of prawn-rails, I'm supposed to create the method in the controller, create a view of it with the same name as the method, and fill in that view.

Method in Posts Controller

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


This is the error that comes up: (screenshot)

enter image description here

EDIT

Routes

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


EDIT 2

Posts Controller

# == 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


Users Controller

# == 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


EDIT

enter image description here

EDIT AGAIN

enter image description here

Answer

According to you assosciation post belongs_to user, so it should be one user who has many posts, so the method should be,

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

Send the user_id from the above link which we will use in posts method,

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

Change @post to @posts

Also add a condition in posty.pdf.prawn file as,

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

This will work for you.