Users crud - stevendevs/ALPHA-BLOG GitHub Wiki
Primero debes crear una cuenta
rails c
email_address = "[email protected]"
Usar el image hash
hash = Digest::MDS.hexdigest(email_address)
image_src = "https://www.gravatar.com/avatar/#{hash}"
pegarlo en la web y para cambiar el tamaño ?s=200 por ejemplo
<% gravatar_for @user %>
Error porque no se ha creado gravatar_for method
app/helper/application_helper.rb
def gravatar(user, options = { size:80 })
email_address = user.email.downcase
hash = Digest::MDS.hexdigest(email_address)
size = options[:size]
gravatar_url = "https://www.gravatar.com/avatar/#{hash}?s={size}"
image_tag(gravatar_url, alt: user:name)
end
80 es el por defecto pero en el show puedo cambiarlo
<% gravatar_for @user, size: 200 %>
rails c
user = User.find(3)
user.email = "[email protected]"
user.save
La imaagen debe cambiar si se recarga
Hay que tomar el mismo formato de articles con sus respectivas accions y agregarlo al show page
crear un archivo _article.html.erb en articles folder y renderizarlo en index de articles
<%= render "article" %>
Debe funcnionar de la misma manera que antes
una vez creado el parcial en articles folder y renderizarlo en el profile del user o show
<%= render "articles/article" %>
No va a funcnionar
@articles = @user.articles
<%= render "articles/article", obj: articles %>
Hacer commits de los cambios
en el controlador de usuarios
def index
@users = User.all
end
Luego crear una vista en views/users/index tambien se puede agregar link a los usuarios
<%= link_to "Bloggers", users_path %>
<%= link_to gravatar_for(user, size: 150) %>
<%= pluralize(user.articles.count, "article") %>
<%= link_to "View Profile", user %>
<%= link_to "Edit profile", edit_user_path(user) %>
<%= time_ago_in_words(article.created_at) %>
<%= time_ago_in_words(user.created_at) %>
<%= link_to "Edit youy profile", edit_user_path(quser) %>
redirect_to user_path
o
@user
<%= if article.user %>
<%= link_to article.user.name, user_path(article.user) %>
<%= end %>
gem 'will_paginate', '~> 4.0'
@article = Article.paginate(page: params[:page], per_page: 50)
en views de articles
<%= will_paginate @articles, container: => false %>
@article = @user.articles.paginate(page: params[:page], per_page: 50)
<%= will_paginate @users, container: => false %>
- Added routes for login - new, create and destroy (to represent login form, logging in a user and logging out a user).
contruir la ruta, no es una ruta por defecto en el resfull
get "login", to: "sessions#new"
post "login", to: "sessions#create" # handle the form submision
destroy "logout", to: "session"destroy"
def new
end
def create
end
def destroy
end
- Added a sessions folder under app/views and created a new.html.erb template to display the login form.
new.html.erb
<%= render "shared/errors", obj: @user %>
<%= form_with(scope: :session, url: login_path , local: true, data: { turbo: false }) do |f| %>
<div class="mb-6">
<%= f.label :email, class: "block text-sm font-medium text-gray-700 mb-1" %>
<%= f.email_field :email, placeholder: "Email address", class: "w-full p-2 border border-gray-300 rounded" %>
</div>
<div class="mb-6">
<%= f.label :password, class: "block text-sm font-medium text-gray-700 mb-1" %>
<%= f.password_field :password, placeholder: "Enter a password", class: "w-full p-2 border border-gray-300 rounded" %>
</div>
<%= f.submit "Sign up", class: "text-white bg-purple-700 hover:bg-purple-800 font-medium rounded-full text-sm px-5 py-2.5 text-center mb-2" %>
<%= link_to "Cancell", articles_path,
class: "py-2.5 px-5 me-2 mb-2 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-full border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700" %>
<% end %>
buebug
params
en el param hash está el email y password
params[:session][:email]
params[:session][:password]
continue
Si funcniona todo está bien
rails c
Ahora definir el usuario
user = User.find_by(email: params[:session][:email] )
Para demostracion en consola se puede solo hacer también solo lo siguiente
user = User.find_by(email: "[email protected]" )
para password
User.authenticate("passowrd")
Tambien se puede hacer el param hash si hay error da nil con correo y con contraseña da false
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
flash[:notice] = "Logged in successfully"
redirect_to user
else
flash.now[:alert] = "There was something wrong with your login details"
render 'new'
end
end
si no existe
En el parcioal de navegacion
<%= link_to 'Log in', login_path %>
</li>
<li class="nav-item">
<%= link_to 'Logout', logout_path, method: :delete %>
destroy
session[:user_id] = nil
flash[:notice] = "Logged out"
redirect_to root_path
end
Link a la documentacion de sessiones
Nos servirá para mostrar o no cosas según si el usuario está authenticado o no
- Modified the navigation partial and utilized these methods to display links based on logged in state.
- Added set_user method and used it as before_action for show, edit and update actions in users controller.