Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD Implementation for Post model #13

Merged
merged 9 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)

rubocop (1.62.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base

end
1 change: 1 addition & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class MainController < ApplicationController
before_action :authenticate_user!
def home
@posts = Post.all
end
end
60 changes: 60 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :set_post, only: %i[show destroy edit update]
def new
@post = Post.new
end

def create
@post = current_user.posts.build(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to root_path, notice: 'Post created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

def index
@posts = Post.user_post(current_user)
end

def edit; end

def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to root_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

def destroy
@post.destroy

respond_to do |format|
format.html { redirect_to root_path, notice: 'Post was succesfully deleted.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.user_post(current_user).find(params[:id])
end

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:body)
end
end
61 changes: 61 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class UsersController < ApplicationController
before_action :authenticate_user!

# GET /users or /users.json
def index
@users = User.all
end

# GET /users/1 or /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users or /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to user_url(@user), notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1 or /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_url(@user), notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name, :lastName, :birthday, :weight, :height, :email, :image_profile)
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Post < ApplicationRecord
belongs_to :user

scope :user_post, ->(user) { where(user_id: user.id) }

has_one_attached :post_image
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class User < ApplicationRecord
has_one_attached :image_profile
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :posts

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>S10a1ProyectoFinalTeam03S10a1ProyectoFinal</title>
<title>GYMRAT APP</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
Expand Down
30 changes: 28 additions & 2 deletions app/views/main/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
<h1>Main#home</h1>
<p>Find me in app/views/main/home.html.erb</p>
<h1>Feed del usuario <%= current_user.id %></h1>
<%= Date.today %>

<%= button_to 'Sign Out', destroy_user_session_path, method: :delete %>


<%= link_to new_post_path do%>
<div style="margin-top: 1rem;">
<span>Create a new post</span>
</div>
<%end%>
<% @posts.each do |post| %>
<div class='post' style="background-color: antiquewhite;">
<h2> <%=post.body%> </h2>
<h3> Este es el post <%=post.id%> y pertenezo al usuario <%=post.user_id%> </h3>
<%= link_to post_path(post.id) do%>
<span>Details</span>
<%end%>

<%= link_to edit_post_path(post.id) do%>
<div>Edit</div>
<%end%>

<%= button_to post_path(post.id), method: :delete do%>
<span>Delete</span>
<%end%>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2 class="mt-0"><%= title %></h2>
<%= form_with(model: @post) do |form| %>

<div>
<%= form.label :body, style: "display: block" %>
<%= form.text_field :body %>
</div>

<div>
<%= form.submit %>
</div>

<% end %>
1 change: 1 addition & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', event: @event, title: 'Edit post' %>
11 changes: 11 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Post: </h1>

<%= Date.today %>
<% @posts.each do |post| %>
<div class='post'>
<h2> <%=post.body%> </h2>
<%= link_to posts_path(post.id) do%>
<h4>Detalles</h4>
<%end%>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', event: @event, title: 'New post' %>
2 changes: 2 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Publicación de <%= current_user.email %></h1>
<h2><%[email protected]%></h2>
47 changes: 47 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%= form_with(model: user) do |form| %>
<% if user.errors.any? %>
<div style="color: red">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% user.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :name, style: "display: block" %>
<%= form.text_field :name %>
</div>

<div>
<%= form.label :lastName, style: "display: block" %>
<%= form.text_field :lastName %>
</div>

<div>
<%= form.label :birthday, style: "display: block" %>
<%= form.date_field :birthday %>
</div>

<div>
<%= form.label :weight, style: "display: block" %>
<%= form.text_field :weight %>
</div>

<div>
<%= form.label :height, style: "display: block" %>
<%= form.text_field :height %>
</div>

<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
32 changes: 32 additions & 0 deletions app/views/users/_user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div id="<%= dom_id user %>">
<p>
<strong>Name:</strong>
<%= user.name %>
</p>

<p>
<strong>Lastname:</strong>
<%= user.lastName %>
</p>

<p>
<strong>Birthday:</strong>
<%= user.birthday %>
</p>

<p>
<strong>Weight:</strong>
<%= user.weight %>
</p>

<p>
<strong>Height:</strong>
<%= user.height %>
</p>

<p>
<strong>Email:</strong>
<%= user.email %>
</p>

</div>
10 changes: 10 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing user</h1>

<%= render "form", user: @user %>

<br>

<div>
<%= link_to "Show this user", @user %> |
<%= link_to "Back to users", users_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Users</h1>

<div id="users">
<% @users.each do |user| %>
<%= render user %>
<p>
<%= link_to "Show this user", user %>
</p>
<% end %>
</div>

<%= link_to "New user", new_user_path %>
9 changes: 9 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New user</h1>

<%= render "form", user: @user %>

<br>

<div>
<%= link_to "Back to users", users_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @user %>

<div>
<%= link_to "Edit this user", edit_user_path(@user) %> |
<%= link_to "Back to users", users_path %>

<%= button_to "Destroy this user", @user, method: :delete %>
</div>
1 change: 0 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
# Store files on Amazon S3.
config.active_storage.service = :amazon


# Compress CSS using a preprocessor.
# config.assets.css_compressor = :sass

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
devise_for :users
root 'main#home'

resources :posts
end
Loading
Loading