Getting current_user undefined method while using devise gem - Ruby on Rails 4.1.5 -


i'm following tutorial: https://www.youtube.com/watch?v=7-1hcwbu7iu did pintrest clone using devise gem , had no issue. when try add current_user links controller "undefined method current_user". tried everything... redoing app scratch, defining current_user, resetting database. guess gem should handle method automatically, right?

here 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' gem file , run bundle install 5. run rails g devise:install 6. add file development.rb - config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } 7. add alerts 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 localhost:3000/users/sign_up (works ok) 13. add relation 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 rails console , save assocation between user , links. 17. add current_user links controller - getting undefined 'curren_user method' once go localhost:3000/links/new 

here 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 |format|       if @link.save         format.html { redirect_to @link, notice: 'link       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 |format|       if @link.update(link_params)         format.html { redirect_to @link, notice: 'link 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 |format|        format.html { redirect_to links_url, notice: 'link 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 , :omniauthable   devise :database_authenticatable, :registerable,          :recoverable, :rememberable, :trackable, :validatable   has_many :links end 

you getting undefined method 'current_user' in new action calling link.current_user.

def new   @link = link.current_user.links.build end 

should be

def new   @link = current_user.links.build end 

link not have current_user method, problem. current_user defined in context of controller , should used in create action.


Comments