I think this is a bit of a newbie question but I’m struggling to do some basic stuff after adding Hotwire. I possibly shouldn’t have added it yet because I’m not actively/deliberately using any of its features but I assumed I could add it to the app and then gradually opt in to extra functionality.
Here’s what I’ve done so far:
- brand new Rails 6.1.3 app created with
rails new my_app -d postgresql
- replaced
turbolinks
gem withhotwire-rails
(version 0.1.3 according to Gemfile.lock) - ran
./bin/rails hotwire:install
- added flash message display to the main layout
- created a pretty standard log in system
routes.rb:
...
get '/sign_in', to: 'sessions#new'
post '/sign_in', to: 'sessions#create'
delete '/sign_out', to: 'sessions#destroy'
...
app/helpers/sessions_helper.rb:
module SessionsHelper
def sign_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= Staff.find_by(id: session[:user_id]) if session[:user_id]
end
def signed_in?
!current_user.nil?
end
def sign_out
session.delete(:user_id)
@current_user = nil
end
end
app/controllers/sessions_controller.rb:
def create
user = Staff.find_by(school_email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to (cookies[:return_to] || root_url), notice: 'Welcome!'
cookies[:return_to] = nil
else
flash.now[:alert] = 'Email address not found or password doesn't match'
render 'new', status: :unprocessable_entity
end
end
I’m now trying to do fairly standard things that I used to be able to do and running into issues, particularly around form submissions, errors and flash. Sign in and out work but failed sign in attempts aren’t behaving the way I’m used to.
Before adding status: :unprocessable_entity
on the failed attempt there was no feedback to suggest sign in failed (no flash message and no re-rendering). Since adding that I can at least get the failed sign in to render but the flash messages still won’t show up.
Should I just opt out of Turbo for this form? Or am I completely misunderstanding what Hotwire does? Perhaps I should remove it until there’s some more help out there for novices and newbs?