0

The following code does not work. It says undefined method 'table_name' for nil:NilClass

@members = Members.all

table member_list_rows do      
  if @members.table_name == members
    row(0).background_color = '3498db'
  end
end

Full code

class MemberPdf < Prawn::Document
  def initialize(members, view, allcount)
    super(top_margin: 50)
    if members.size != allcount
      @warn = " (Not all members)"
    else
      @all = " All"
    end
    text "Showing#{@all} #{members.size} Members", size: 18, style: :bold, align: :center, color: "636363"
    text "#{@warn}", size: 11, align: :center, color: "858585"
    @members = members
    @view = view
    member_list
  end

  def member_list
    move_down 20
    table member_list_rows do
      self.row(0).align = :center
      if @members.table_name == "members"
        row(0).background_color = '3498db'
      else

      end

      row(0).text_color = "FFFFFF"
      self.row_colors = ["DDDDDD", "FFFFFF"]
      self.header = true
      #self.cell.text_color = "B3B3B3"
      row(0).columns(0).style size: 20
    end
  end

  def member_list_rows
    [["Name", "Awardunit", "Address", "Contact", "Level of Entry", "Current Award", "Disabled?"  ]] +
    @members.map do |member|
      [member.name, member.awardunit.name, member.address, member.name, member.entrylvl, member.currentaward, @view.yesno(member.disabled)]
    end
  end
end

Members controller

    if params[:commit] == "Clear"
        params[:q] = nil
    end

    respond_to do |format|
        format.html
        format.pdf do
            pdf = MemberPdf.new(Member.search(params[:q]).result.order( 'name ASC' ), view_context, Member.all.size)
            send_data pdf.render, filename: "Members_List.pdf", type: "application/pdf", disposition: "inline"
        end
    end
1
  • Maybe you can puts the initial text to the console instead of using text in your PDF-file, that might allow you to see how many members you have. However, I still fail to see where table_name should be defined. @members will just hold an Array which will not answer to your table_name even if it is not nil. Maybe you can add another puts @members.count inside member_list_rows to help with debugging? Commented Mar 25, 2014 at 9:46

3 Answers 3

3

It is due to @members is nil.You are doing it wrong.

Change this

@members = Members.all #Wrong

to

@members = Member.all #Right

Always remember,the Model name should be singular.

Those are called Naming Conventions. For more information,read these Style guides(Ruby and Rails)

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, its a mistake in the question. In the actual code its correct
Can you Post the full code where that Snippet belongs to?
Where do you have this line @members = Member.all? In your members_controller?
Yes. Then it pass members to this class. This piece of code use that data to render a list of member. The member data is coming to this class and im sure of it because the list gets rendered.
@EApubs Ok Can you please post your members_controller also?
|
1

Most likely table method is changing context, in which you don't have access to the @members instance variable anymore. This can be achieved easily by this sample code:

def do_stuff(&block)
  cls = Class.new
  cls.instance_eval(&block)
end

@test_var = "test_var"

do_stuff { puts @test_var }

You will receive nothing, because @test_var does not exist in the cls.

1 Comment

Reading the Prawn source code it seems you are quite right.
0

Am not sure what you are doing with "table member_list_rows". Didn't get that. In the third line though, it should be

if @members.table_name == "members"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.