Devise project: https://github.com/heartcombo/devise?tab=readme-ov-file
Steps
Add the bundle and install
bundle add devise
rails generate devise:installCode language: CSS (css)
Now edit config/environments/development.rb to set up the mailer
config.action_mailer.default_url_options = { host: '<em>138.28.162.211</em>', port: 3002 }Code language: HTML, XML (xml)
use your VM’s ip address.
New we need to add the INDEX to your rails mysql user:
mysql -u root -p
GRANT INDEX ON *.* TO'youruser'@'%';
exit;Code language: PHP (php)
New create the user table (you can use or create any model you want, I am using user here:
rails generate devise user
Now create the model
rails db:migrateCode language: CSS (css)
Edit the application wrapper page: app/views/layout/application.html.erb and replace the current login and sign up butttons with:
<form class="">
<%= link_to 'Log In', new_user_session_path, class:"btn btn-outline-success"%>
<%= link_to 'Become member', new_user_registration_path, class:"btn btn-outline-success"%>
</form>Code language: JavaScript (javascript)
This calls the default divise login and registration options. Now we don’t want the buttons to show up if we are logged in. And we do want to show the user in app/views/layout/application.html.erb:
<form class="">
<% if !user_signed_in? %>
<%= link_to 'Log In', new_user_session_path, class:"btn btn-outline-success"%>
<%= link_to 'Become member', new_user_registration_path, class:"btn btn-outline-success"%>
<% else %>
<%= current_user.email %>
<%= link_to 'Sign out', destroy_user_session_path, class:"btn btn-outline-success"%>
<% end %>
</form>Code language: HTML, XML (xml)
We need to add to the routes.rb for this to work:
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
endCode language: PHP (php)
Now we want to limit what the user can do based on the login status. For each of the controllers:
- articles_controller.rb
- comments_controller.rb
- keywords_controller.rb
We want to add a line at the top (Remove the https login lines if they are there):
class ArticlesController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
class KeywordsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
class CommentsController < ApplicationController
before_action :authenticate_user!
Code language: CSS (css)
These new lines limit what the user can do to index and show (only create and destroy existinin comments, so we have less there)
Now you are forced to login before adding, changing, or deleting anything.
Now – since we have log in to make comments, we can pre-prime the commenter to be the email address of the commenter. We want to make it not editable so they must use their email (app/view/comments/_form.html.erb):
<%= form_with model: [ @article, @article.comments.build ] do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter, value: current_user.email, :disabled=>true %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.label :status %><br>
<%= form.select :status, Visible::VALID_STATUSES, selected: 'public' %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>Code language: HTML, XML (xml)
You can, now the the users model exist, add fields, such as name, address, interests, etc. Then you can crearte a user profile page for each user, that the user can manage. I leave that up to you.
