-1

I seem to have gotten things to a point in my rails app making sure that games and tickets are being created, but when I try and display them in the html.erb file, the whole database is being rendered...all I'm looking to display is the actual tickets attributes. Here is what it looks like and the code associated with it. Any help is appreciated.

enter image description here

The code is:

 <h1> game id is <%= @g.id %> </h1>

 <h2> Ticket values are: </h2>

 <table>
     <%= @g.tickets.each do |tick| %>

     <tr>
         <td><%= tick.id %></td>
         <% if tick.nickname.blank? %>
         <td> Available</td>
         <% else %>
         <td><%= tick.nickname %></td>
         <% end %>
         <td><%= tick.game_id %></td>
     </tr>
     <% end %>
 </table>

I need to remove the full database portion that is printed out under ticket values, but cant' seem to have it removed without removing everything. Thanks for your help.

The tickets controller is:

  class TicketsController < ApplicationController

    def create
      @ticket = Ticket.new
      @ticket.nickname = params["nickname"]
      @ticket.game_id = params["game_id"]
      @ticket.save
    end

  end

The games controller is:

 class GamesController < ApplicationController

 def create
    @g = Game.new
    @g.winning_ticket_num = params["winning_ticket_num"]
    @g.value_per_ticket = params["value_per_ticket"]
    @g.save
    10.times do 
      @g.tickets.create
    end
    render 'games/show'
 end 


 def new
 end

 def show 
 end


 end
user1572597
  • 373
  • 3
  • 15

2 Answers2

1

Remove = from

<%= @g.tickets.each do |tick| %>

It should look like this

<% @g.tickets.each do |tick| %>
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

The problem is with this line

<%= @g.tickets.each do |tick| %>.It outputs all the tickets that are associated witha game.

Remove = and the code should be like this

<table>
     <%@g.tickets.each do |tick| %>

     <tr>
         <td><%= tick.id %></td>
         <% if tick.nickname.blank? %>
         <td> Available</td>
         <% else %>
         <td><%= tick.nickname %></td>
         <% end %>
         <td><%= tick.game_id %></td>
     </tr>
     <% end %>
 </table>

Always be careful in using these Rails expressions. <% %> executes the output.whereas <%= %> prints the output.

Pavan
  • 33,316
  • 7
  • 50
  • 76