Reputation: 3
I'm following tutorial: https://www.youtube.com/watch?v=7-1HCWbu7iU I already did Pintrest clone using devise gem and had no issue. When I try to add current_user to links controller I get "undefined method current_user". I tried everything... even redoing the app from scratch, defining current_user, resetting the database. I guess the gem should handle the method automatically, right?
Here are my steps:
1. Generate new app - rails new raddit
2. Generate scaffold: rails g scaffold link title:string url:string
3. rake db:migrate
4. add gem 'devise', '~> 3.3.0' to gem file and run bundle install
5. run rails g devise:install
6. add file to development.rb - config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
7. add alerts to layout/application.html (as per devise documentation)
8. generate view file - rails g devise:views
9. generate user - rails g devise User
10. rake db:migrate
11. rails s
12. go to localhost:3000/users/sign_up (works ok)
13. Add relation to models: user.rb - has_many :links ; link.rb - belongs_to :user
14. Create migration: rails generate migration add_user_id_to_links user_id:integer:index
15. rake db:migrate
16. go to rails console and save assocation between user and links.
17. Add current_user to links controller - Getting undefined 'curren_user method' once I go to localhost:3000/links/new
Here is my code:
class LinksController < ApplicationController
before_action :set_link, only: [:show, :edit, :update, :destroy]
def index
@links = Link.all
end
def show
end
def new
@link = Link.current_user.links.build
end
def edit
end
def create
@link = current_user.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
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
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
def set_link
@link = Link.find(params[:id])
end
def link_params
params.require(:link).permit(:title, :url)
end
end
My models: link.rb
class Link < ActiveRecord::Base
belongs_to :user
end
My models: user.rb
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 :links
end
Upvotes: 0
Views: 1192
Reputation: 7361
it should be
@link = current_user.links.build
in new action
if you have more problem than just type exit in new action and check the current_user in console.. You can get errors from console.. so every time check your console first.
use better_errors gem.. for more info about the particular error
Upvotes: 0
Reputation: 494
You are getting the undefined method 'current_user' in the new action where you are calling Link.current_user.
def new
@link = Link.current_user.links.build
end
should be
def new
@link = current_user.links.build
end
Link does not have a current_user method, that is the problem. current_user is defined in the context of the controller and should be used as you do in the create action.
Upvotes: 3