Devise authentication gem: How to save the logged in user id?
Create a migration file with a column of type
integer
nameduser_id
, this is where the association will be stored. (see the migration guide for details on how to do this).in your model, add:
belongs_to :user
(see the associations guide for more details)in the controller for your model, add
@your_model.user = current_user
in the create action. This will do the association you're looking for. (current_user
is a method provided by devise that returns the User ActiveRecord for the currently logged in user)
Note: there are more than one way of doing the actual association. I'm suggesting to do it in the controller but it could be done in the model or elsewhere.
Get current user id in devise
Replace current index action as below
def index
if current_user
@sheets = current_user.time_sheets
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
If user is not logged in, i.e., current_user=nil then user would be redirected to login page with a flash message.
Ruby on Rails: gem devise session logged in
Try this in your User Controller:
@user = User.new(user_params)
@user.company_id = current_company.id
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Rails Devise: How to access sign up page after signed in?
The way I handled the scenario of being able to create New Users if you are signed in was by generating a User controller and having new and create action methods that would save to the User model. (This was stripped from a current app I'm working on so hopefully I didn't miss anything)
user_controller.rb
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Successfully created User."
redirect_to root_path
else
render :action => 'new'
end
end
views/user/new.html.erb
<%= form_for @user, :url => user_index_path do |f| %>
<p><%= f.label :email %>
<%= f.text_field :email %></p>
<p><%= f.label :password %>
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Save" %></p>
<% end %>
config/routes.rb (Rails 3)
resources :user, :controller => "user"
Link to the New User page
<%= link_to 'New User', new_user_path %>
RoR Devise: Sign in with username OR email
I have found a solution for the problem. I'm not quite satisfied with it (I'd rather have a way to specify this in the initializer), but it works for now. In the user model I added the following method:
def self.find_for_database_authentication(conditions={})
find_by(username: conditions[:email]) || find_by(email: conditions[:email])
end
As @sguha and @Chetan have pointed out, another great resource is available on the official devise wiki.
Related Topics
How to Stop God from Leaving Stale Resque Worker Processes
How to Disable a Form Submit Button "A Là Ruby on Rails Way"
How to Test If All Items in an Array Are Identical
Good Explanation of Ruby Object Model -- Mainly, 'Classes Are Objects'
Is the Unix Philosophy Falling Out of Favor in the Ruby Community
What Grammar Based Parser-Generator Tools Exist for Ruby
How to Do Sti and Still Use Polymorphic Path Helpers
Carrierwave Fog Amazon S3 Images Not Displaying
Is There a Ruby Http Client Library with a Response Cache
Dynamically Defined Setter Methods Using Define_Method
Appending Headers to Rspec Controller Tests
Adding a "Like/Unlike" Button to a Post in Rails
Ruby 2.0.0 String#Match Argumenterror: Invalid Byte Sequence in Utf-8