Devin
Devin

Reputation: 1021

Rails link_to the records of a instance variable .count() result

I have records in my Submitter model being shown in Application Index view being displayed in a table like below. Only I want to be able to click on the number that's listed for the .count() and be taken to my Tickets Index view with all of the corresponding records (Tickets) listed.

I'm having trouble understanding how I can link instance variables to a controller's action, in order to use the layout of my views.

Any ideas?

<table>
  <tr>
    <th>Name</th>
    <th>Number of Tickets</th>
  </th>
  <tr>
    <% Submitter.where(:school_id => current_user.school_id, :disabled => false).each do |sub| %>
    <td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
    <td><%= Ticket.where(:submitter_id => sub).count %></td>
  </tr>
    <% end %>
</table>

routes.rb

resources :feedbacks
resources :products

get "password_resets/new"
get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'

resources :users
resources :sessions
resources :password_resets
resources :mailing_lists
resources :submitters
resources :emails
resources :notes
resources :reminders
resources :descriptions
resources :issues
resources :locations
resources :schools
resources :tickets

root :to => "application#index"

Upvotes: 0

Views: 762

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18070

It sounds like you have a submitter that has many tickets. If so, you'd show a list of submitters (in the submitters_controller#index method) and count the number of tickets. Then link that number to the tickets_controller#index method. Like this...

routes.rb

resources :submitters do
  resources :tickets
end

submitters_controller.rb

class SubmittersController < ApplicationController

  def index
    @submitters = Submitter.where(:school_id => current_user.school_id, :disabled => false)
  end

end

tickets_controller.rb

class TicketsController < ApplicationController

  def index
    @submitter = Submitter.find(params[:submitter_id])
    @tickets = @submitter.tickets
  end

end

views/submitters/index.html.erb

<table>
  <tr>
    <th>Name</th>
    <th>Number of Tickets</th>
  </th>
  <% @submitters.each do |sub| %>
    <tr>
      <td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
      <td><%= link_to pluralize(sub.tickets.count, 'ticket'), submitter_tickets_path(sub) %></td>
    </tr>
  <% end %>
</table>

views/tickets/index.html.erb

# This would show a list of tickets and their attributes - possibly linking to a ticket.

Upvotes: 1

Farley Knight
Farley Knight

Reputation: 1793

Something like this maybe? Not 100% understanding your question.

<table>
  <tr>
    <th>Name</th>
    <th>Number of Tickets</th>
  </th>
  <tr>
    <% Submitter.where(:school_id => current_user.school_id, :disabled => false).each do |sub| %>
      <td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
      <td><%= link_to pluralize(sub.tickets.count, 'ticket'), tickets_path %></td>
    <% end %>
  </tr>
</table>

I, of course, can't give you perfectly working code unless I know what your config/routes.rb looks like, since I don't know what is the method for your Tickets Index view.

Upvotes: 0

Related Questions