Skip to content

Instantly share code, notes, and snippets.

@dylanerichards
Last active January 2, 2016 02:48
Show Gist options
  • Save dylanerichards/8239360 to your computer and use it in GitHub Desktop.
Save dylanerichards/8239360 to your computer and use it in GitHub Desktop.
undefined method `first_name' for nil:NilClass Error is in show.html.erb on line 4
<%= simple_form_for(@comment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :body %>
<%= f.input :guide_id %>
<%= f.input :user_id %>
</div>
<%= f.hidden_field :guide_id %>
<%= f.hidden_field :user_id %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :guide
end
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@comment = Comment.new(comment_params.merge({:user_id => current_user.id}))
respond_to do |format|
if @comment.save
format.html { redirect_to :back, notice: 'Comment was successfully created.' }
format.json { render action: 'show', status: :created, location: @comment }
else
format.html { render action: 'new' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:body, :guide_id, :user_id)
end
end
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :body
t.integer :guide_id
t.integer :user_id
t.timestamps
end
add_foreign_key :comments, :guides
end
end
class GuidesController < ApplicationController
before_action :set_guide, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /guides
# GET /guides.json
def index
if params[:tag]
@guides = Guide.tagged_with(params[:tag])
else
@guides = Guide.all
end
end
# GET /guides/1
# GET /guides/1.json
def show
@guide = Guide.find(params[:id])
@comments = guide.comments
@comment = comments.build
end
# GET /guides/new
def new
@guide = current_user.guides.build(guide_params)
end
# GET /guides/1/edit
def edit
end
# POST /guides
# POST /guides.json
def create
@guide = current_user.guides.build(guide_params)
respond_to do |format|
if @guide.save
format.html { redirect_to @guide, notice: 'Guide was successfully created.' }
format.json { render action: 'show', status: :created, location: @guide }
else
format.html { render action: 'new' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guides/1
# PATCH/PUT /guides/1.json
def update
respond_to do |format|
if @guide.update(guide_params)
format.html { redirect_to @guide, notice: 'Guide was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guide.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guides/1
# DELETE /guides/1.json
def destroy
@guide.destroy
respond_to do |format|
format.html { redirect_to guides_url }
format.json { head :no_content }
end
end
def upvote
@guide = Guide.find(params[:id])
@guide.liked_by current_user
redirect_to :back
end
def downvote
@guide = Guide.find(params[:id])
@guide.downvote_from current_user
redirect_to :back
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guide
@guide = Guide.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guide_params
params.require(:guide).permit(:title, :author, :description, :link, :tag_list) if params[:guide]
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140103130329) do
create_table "comments", force: true do |t|
t.text "body"
t.integer "guide_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "favorite_guides", force: true do |t|
t.integer "guide_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "guides", force: true do |t|
t.string "title"
t.string "author"
t.text "description"
t.string "link"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
add_index "guides", ["user_id"], name: "index_guides_on_user_id"
create_table "taggings", force: true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: true do |t|
t.string "name"
end
create_table "users", force: true do |t|
t.string "first_name"
t.string "runescape_username"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: true do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "voter_id"
t.string "voter_type"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end
<h3>Comments</h3>
<% @guide.comments.each do |comment| %>
<div>
<strong><%= comment.user.first_name %></strong> :
<p><%= comment.body %></p>
</div>
<% end %>
<%= render 'comments/form' %>
class User < ActiveRecord::Base
has_many :guides
has_many :comments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment