Skip to content

Instantly share code, notes, and snippets.

@dylanerichards
Last active December 30, 2015 23:49
Show Gist options
  • Select an option

  • Save dylanerichards/7902811 to your computer and use it in GitHub Desktop.

Select an option

Save dylanerichards/7902811 to your computer and use it in GitHub Desktop.
User returning nil. On line 17 of index.html.erb. Undefined method "username" for nilNilclass
<%= render 'pages/home' if !user_signed_in? %>
<div id="things" class="transitions-enabled">
<% @things.each do |thing| %>
<div class='panel panel default'>
<div class="box">
<%= link_to image_tag(thing.image.url(:medium)), thing %>
<div class='panel-body'>
<% if thing.link.blank? %>
<strong><%= thing.title %></strong>
<% else %>
<strong><%= link_to thing.title, "http://#{thing.link}"%></strong>
<% end %>
<p><%= thing.description %></p>
By <%= link_to thing.user.username, user_path(thing.user) %>
<% if thing.user == current_user %>
<%= link_to edit_thing_path(thing) do %>
<span class='glyphicon glyphicon-edit'></span>
<% end %>
<%= link_to thing_path(thing), method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class='glyphicon glyphicon-trash'></span>
<% end %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
<%= will_paginate @posts, renderer: BootstrapPagination::Rails, class: 'pull-left' %>
Started GET "/" for 127.0.0.1 at 2013-12-10 20:02:52 -0500
Processing by ThingsController#index as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
Thing Load (0.3ms) SELECT "things".* FROM "things" ORDER BY created_at DESC LIMIT 50 OFFSET 0
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered things/index.html.erb within layouts/application (12.7ms)
Completed 500 Internal Server Error in 19ms
ActionView::Template::Error (undefined method `username' for nil:NilClass):
14: <% end %>
15:
16: <p><%= thing.description %></p>
17: By <%= link_to thing.user.username, user_path(thing.user) %>
18:
19: <% if thing.user == current_user %>
20: <%= link_to edit_thing_path(thing) do %>
app/views/things/index.html.erb:17:in `block in _app_views_things_index_html_erb___1232011580397462783_2192440640'
app/views/things/index.html.erb:4:in `_app_views_things_index_html_erb___1232011580397462783_2192440640'
Rendered /Users/DylanRichards/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
Rendered /Users/DylanRichards/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /Users/DylanRichards/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.0ms)
<%= link_to 'Back', :back, class: 'btn btn-xs btn-primary' %>
<div class='row'>
<div class='col-md-offset-2 col-md-8'>
<div class='panel panel-default'>
<div class='panel-heading text-center'>
<%= image_tag @thing.image.url(:medium) %>
</div>
<div class='panel-body'>
<p>
<% if @thing.link.blank? %>
<strong><%= @thing.title %></strong>
<% else %>
<strong><%= link_to @thing.title, "http://#{@thing.link}" %></strong>
<% end %>
</p>
<p>
<%= @thing.description %>
</p>
<p>
By <%= link_to @thing.user.username, @thing.user %>
</p>
<% if @thing.user == current_user %>
<%= link_to edit_thing_path(@thing) do %>
<span class='glyphicon glyphicon-edit'></span>
<% end %>
<% end %>
</div>
</div>
</div>
class Thing < ActiveRecord::Base
belongs_to :user
default_scope -> { order('created_at DESC') }
has_attached_file :image, :styles => { :large => '500x500>', :medium => '300x300>', :thumb => '100x100>' }
validates :image, presence: true
validates :title, presence: true, length: { minimum: 5, maximum: 50 }
# Returns microposts from the users being followed by the given user.
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
class ThingsController < ApplicationController
before_action :set_thing, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /things
# GET /things.json
def index
@things = Thing.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 50)
end
# GET /things/1
# GET /things/1.json
def show
end
# GET /things/new
def new
@thing = current_user.things.build
end
# GET /things/1/edit
def edit
end
# POST /things
# POST /things.json
def create
@thing = current_user.things.build(thing_params)
respond_to do |format|
if @thing.save
format.html { redirect_to @thing, notice: 'Thing was successfully created.' }
format.json { render action: 'show', status: :created, location: @thing }
else
format.html { render action: 'new' }
format.json { render json: @thing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /things/1
# PATCH/PUT /things/1.json
def update
respond_to do |format|
if @thing.update(thing_params)
format.html { redirect_to @thing, notice: 'Thing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @thing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /things/1
# DELETE /things/1.json
def destroy
@thing.destroy
respond_to do |format|
format.html { redirect_to things_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_thing
@thing = Thing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def thing_params
params.require(:thing).permit(:title, :description, :image, :link)
end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, #:recoverable,
:rememberable, :trackable, :validatable
has_many :things
validates :name, presence: true, length: { minimum: 2, maximum: 20}
validates :username, presence: true, length: { minimum: 2, maximum: 20}
validates :username, uniqueness: true
validates :email, presence: true
validates :email, uniqueness: true
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" , :nav => "25x25"}
extend FriendlyId
friendly_id :username
def show
end
#follow features
has_many :followed_users, through: :relationships, source: :followed
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
def following?(other_user)
relationships.find_by(followed_id: other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy!
end
def feed
Thing.from_users_followed_by(self)
end
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
end
class UsersController < ApplicationController
def show
@user = User.find_by_username(params[:id])
end
def user_params
params.require(:user).permit(:avatar)
end
def following
@title = "Following"
@user = User.find_by_username(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.find_by_username(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment