Skip to content

Instantly share code, notes, and snippets.

@Mizpah
Created February 25, 2013 23:31
Show Gist options
  • Save Mizpah/5034350 to your computer and use it in GitHub Desktop.
Save Mizpah/5034350 to your computer and use it in GitHub Desktop.
Initially built with scaffold, edit and show links were working. After working extensively on index (ajaz, paging, sorting, search fielkds, partials), I have discovered they are no longer working! For example the show.html.erb gives me: undefined method `name' for nil:NilClass when executing <%= @game_class.name % However, the show method is def…
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<table class="pretty">
<thead>
<tr>
<th><%= sortable "name" %></th>
<th>Description</th>
<th><%= sortable "game_id" %></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<% for game_class in @game_classes %>
<tr>
<td><%= game_class.name %></td>
<td><%= game_class.description %></td>
<td><%= game_class.game_id %></td>
<td><%= link_to 'Show', game_class %></td>
<td><%= link_to 'Edit', edit_game_class_path(game_class) %></td>
<td><%= link_to 'Destroy', game_class, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<div id="paginator">
<%= paginate @game_classes %>
</div>
class GameClassesController < ApplicationController
# GET /game_classes
# GET /game_classes.json
# added to allow controller to generate parameters if not present. railscast 228
helper_method :sort_column, :sort_direction
def index
@game_classes = GameClass.search(params[:search]).order(sort_column + ' ' + sort_direction).page(params[:page]).per(5)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @game_classes }
format.js # index.js.erb
end
end
# Methods to set sort peramiters securely (checking whats in the query string)
private
def sort_column
GameClass.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
# GET /game_classes/1
# GET /game_classes/1.json
def show
@game_class = GameClass.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @game_class }
end
end
# GET /game_classes/new
# GET /game_classes/new.json
def new
@game_class = GameClass.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @game_class }
end
end
# GET /game_classes/1/edit
def edit
@game_class = GameClass.find(params[:id])
end
# POST /game_classes
# POST /game_classes.json
def create
@game_class = GameClass.new(params[:game_class])
respond_to do |format|
if @game_class.save
format.html { redirect_to @game_class, notice: 'Game class was successfully created.' }
format.json { render json: @game_class, status: :created, location: @game_class }
else
format.html { render action: "new" }
format.json { render json: @game_class.errors, status: :unprocessable_entity }
end
end
end
# PUT /game_classes/1
# PUT /game_classes/1.json
def update
@game_class = GameClass.find(params[:id])
respond_to do |format|
if @game_class.update_attributes(params[:game_class])
format.html { redirect_to @game_class, notice: 'Game class was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @game_class.errors, status: :unprocessable_entity }
end
end
end
# DELETE /game_classes/1
# DELETE /game_classes/1.json
def destroy
@game_class = GameClass.find(params[:id])
@game_class.destroy
respond_to do |format|
format.html { redirect_to game_classes_url }
format.json { head :no_content }
end
end
end
<h1>Listing game_classes</h1>
<%= form_tag game_classes_path, :method => 'get', :id => "game_classes_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="game_classes"><%= render 'game_class' %></div>
<%-end %>
<p><%= link_to "New Class", new_game_class_path %></p>
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= @game_class.name %>
</p>
<p>
<b>Description:</b>
<%= @game_class.description %>
</p>
<%= link_to 'Edit', edit_game_class_path(@game_class) %> |
<%= link_to 'Back', game_classes_path %>
@Mizpah
Copy link
Author

Mizpah commented Feb 26, 2013

An example error url: http://localhost:3000/game_classes/13

Error: undefined method `name' for nil:NilClass

code generating the error:

4: Name:
5: <%= @game_class.name %>

Parameters:

{"id"=>"13"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment