Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialalborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsGetting NoMethodError in Links#index
Hi - I'm going along with a tutorial in order to make a Reddit clone. After adding bootstrap and some styling, I'm getting an error after refreshing the web page.
undefined method `name'
<h2>
<%= link_to link.title, link %><br>
<small class="author">Submitted <%= time_ago_in_words(link.created_at) %> by <%= link.user.name %></small>
</h2>
<div class="btn-group">
Thanks!
alborz
Full Stack JavaScript Techdegree Graduate 30,885 Pointsclass LinksController < ApplicationController
before_action :set_link, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, :except => [:index, :show]
# GET /links
# GET /links.json
def index
@links = Link.all
end
# GET /links/1
# GET /links/1.json
def show
end
# GET /links/new
def new
@link = current_user.links.build
end
# GET /links/1/edit
def edit
end
# POST /links
# POST /links.json
def create
@link = current_user.links.build(link_params)
respond_to do |format|
if @link.save
format.html { redirect_to @link, notice: 'Link was successfully created.' }
format.json { render :show, status: :created, location: @link }
else
format.html { render :new }
format.json { render json: @link.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /links/1
# PATCH/PUT /links/1.json
def update
respond_to do |format|
if @link.update(link_params)
format.html { redirect_to @link, notice: 'Link was successfully updated.' }
format.json { render :show, status: :ok, location: @link }
else
format.html { render :edit }
format.json { render json: @link.errors, status: :unprocessable_entity }
end
end
end
# DELETE /links/1
# DELETE /links/1.json
def destroy
@link.destroy
respond_to do |format|
format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_link
@link = Link.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def link_params
params.require(:link).permit(:title, :url)
end
end
Yes - it was working before adding the Boostrap style changes.
Alex Stophel
iOS Development Techdegree Student 11,552 PointsLooks fine. What does the rest of the code look like in the view?
I imagine you're getting 'link' by doing something like this?
<% @links.each do |link| %>
<% end %>
alborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsThat's correct.
<% @links.each do |link| %>
<div class="link row clearfix">
<h2>
<%= link_to link.title, link %><br>
<small class="author">Submitted <%= time_ago_in_words(link.created_at) %> by <%= link.user.name %></small>
</h2>
<div class="btn-group">
<a class="btn btn-default btn-sm" href="<%= link.url %>">Visit Link</a>
<%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= link.get_downvotes.size %>
<% end %>
</div>
</div>
<% end %>
Alex Stophel
iOS Development Techdegree Student 11,552 PointsEverything looks OK.
Go to the terminal (inside of the project folder) and type rails c
. This will bring up the Rails Console. Inside, type u = Link.first.user
. This will get the first Link in the database and store the user in the variable u
. Now try doing u.name
. Do you get the same error? Or do you get any other errors following these steps?
alborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsLooks like I get the same error: NoMethodError: undefined method `name'
Alex Stophel
iOS Development Techdegree Student 11,552 PointsSorry it took me so long to reply. It sounds like there's not a 'name' attribute on the user model. Can you check db/schema.rb and see if there is column on the users table named 'name'?
If there's not, you'll need to write a migration and add name to the user table: rails g migration AddNameToUser name:string
alborz
Full Stack JavaScript Techdegree Graduate 30,885 PointsThanks - adding the name attribute to the user model worked! If you can post your answer I can upvote it.
1 Answer
Alex Stophel
iOS Development Techdegree Student 11,552 PointsIt sounds like there's not a 'name' attribute on the user model. Can you check db/schema.rb and see if there is column on the users table named 'name'?
If there's not, you'll need to write a migration and add name to the user table: rails g migration AddNameToUser name:string
J.C. Hiatt
9,393 PointsAlex Stophel — I'm getting the same error... This is my schema.rb file:
# 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: 20150525044709) do
create_table "comments", force: :cascade do |t|
t.integer "link_id"
t.text "body"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["link_id"], name: "index_comments_on_link_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"
create_table "links", force: :cascade do |t|
t.string "title"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "links", ["user_id"], name: "index_links_on_user_id"
create_table "users", force: :cascade do |t|
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"
t.string "name"
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: :cascade 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
J.C. Hiatt
9,393 PointsI forked his repo and replaced directories one at a time. The issue is in the db folder. I feel like I followed his tutorial perfectly, but maybe not. I wouldn't know where to begin to figure out where I went wrong there. Maybe I'll just start over again.
Alex Stophel
iOS Development Techdegree Student 11,552 PointsAlex Stophel
iOS Development Techdegree Student 11,552 PointsCan you post your LinkController code?
Also, was this code working before you added Bootstrap and the style changes?