user3382438
user3382438

Reputation: 131

Rails count and display

I am new to rails. I have three models tickets, tags and comments with relationships and it is working fine.

I want to display the total number of tickets in my ticket index view, but I don't know why... I think that this is a really easy answer for you guys...

<%= ticket.count %> says undefined method.

Can you help me or do you need more informations? Thank you!

Upvotes: 1

Views: 1035

Answers (2)

Kirti Thorat
Kirti Thorat

Reputation: 53018

Assuming that in TicketController you have something like this:

  def index
    @tickets = Ticket.all
  end

In your index view, to display the count of tickets, do as follows:

<%= @tickets.count %>
<% @tickets.each do |ticket| %>
   .....

<% end %>

Call the count method on the collection object @ticket(Array of type ActiveRecord::Relation) and not on the ticket which is an instance of Ticket class.

Upvotes: 1

Anil Maurya
Anil Maurya

Reputation: 2328

In controller load ticket count

@ticket_count = Ticket.all.count

in view

<%= @ticket_count %>

ticket.count will not work because ticket is object of Ticket class which does not have count method defined . you can define count method for ticket and compute total of all Ticket then it will surely work.

I suggest to use Ticket.all.count which will return total no of tickets

Upvotes: 1

Related Questions