Reputation: 644
I'm trying to use a resource attribute in one of my internationalized strings used by Devise. I've created a new SessionsController
which inherits from Devise::SessionsController
and in it, I've override the create
method, like:
class SessionsController < Devise::SessionsController
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message!(:notice, :signed_in)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
end
I'm redirecting to an specific path and passing the resource
. I can debug using byebug and I see the resource from the logged user with its attributes, but still I don't know how to pass such attributes in order to be used within a "locale" message, like:
devise:
sessions:
user:
signed_in: 'You have signed in successfully. Welcome %{name}!'
Currently name
doesn't print the value from the user's name, so, I'm wonder how to achieve it.
I'm displaying the messages in a _message.html.erb
partial, like:
<% flash.each do |key, value| %>
<div class="alert alert-icon alert-dismissible alert-<%= key %> flash-alert" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<i class="fa fa-times" aria-hidden="true"></i>
</button>
<%= value %>
</div>
<% end %>
My routes include the SessionsController:
devise_for :users, controllers: { sessions: 'sessions' }, skip: [:registrations]
as :user do
...
post 'login', to: 'sessions#create', as: :user_session
end
end
And also the POST 'login' is pointing to the DeviseSessions#create action and controller.
Upvotes: 2
Views: 562
Reputation: 644
I also did it work just creating a flash[:notice]
message in the create
method:
def create
...
flash[:notice] = I18n.t('devise.sessions.user.signed_in', name: resource.name)
...
end
But @André's answer works also perfectly.
Upvotes: 1
Reputation: 1189
You should pass the name
in the options
params for the set_flash_message!
.
set_flash_message!(:notice, :signed_in, { name: resource.name })
Upvotes: 1