JavierQQ23
JavierQQ23

Reputation: 714

Rails - Devise, is it possible to use user_signed_in? without authenticate_user!?

I'm doing a sort of notepad in rails. the main page has this form for notes with the only field "content". Everyone can create a note but I'm trying to add authentication using devise so users can sign up and save their notes.

So I have my site controller with index as uniq method

site controller

def index
  @note = Note.new
end

and then a notes controller with the create action

notes controller

def create
  @note = if user_signed_in?
    current_user.notes.build(params[:note])
  else
    Note.new(params[:note])
  end

  respond_to do |format|
    if @note.save
      format.js
    else
      format.js
    end
  end
end

I though that would work but devise helpers only seems to work when I add

  before_filter :authenticate_user!

on the controller I need.

is it possible to check if there's a user without the before_filter? or should I make 2 create methods?

Upvotes: 0

Views: 720

Answers (1)

Lucca Mordente
Lucca Mordente

Reputation: 1131

As a workaround you can try to add this before filter:

before_filter :authenticate_user!, :only => []

It might add the helpers (if it's really needed) but won't apply to any action.

Upvotes: 2

Related Questions