Skip to content

Commit

Permalink
Личный кабинет. Часть 2
Browse files Browse the repository at this point in the history
  • Loading branch information
maclen2007 committed Jan 6, 2024
1 parent 805f411 commit 24ddcb4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
19 changes: 19 additions & 0 deletions app/controllers/posts_controller.rb
@@ -1,6 +1,7 @@
class PostsController < ApplicationController

before_action :authenticate_user!, only: [:new]
before_action :set_post, only: [:edit, :show, :update, :destroy]

def index
@posts = Post.all
Expand All @@ -16,6 +17,20 @@ def create
redirect_to posts_path
end

def edit
@post = Post.find(params[:id])
end

def update
@post.update(post_params)
redirect_to post_path(@post)
end

def destroy
@post.destroy
redirect_to posts_path
end

private

def post_params
Expand All @@ -26,4 +41,8 @@ def authenticate_user
redirect_to new_user_session_path if current_user.nil?
end

def set_post
@post = Post.find(params[:id])
end

end
14 changes: 14 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -39,6 +39,20 @@
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>

<% if user_signed_in? %>
<li class="nav-item">
<a class="nav-link" href="<%= url_for(cabinet_path) %>">Личный кабинет</a>
</li>
<% else %>
<li class="nav-item">
<a class="nav-link"href="<%= url_for(new_user_registration_path) %>">Регистрация</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<%= url_for(new_user_session_path) %>">Авторизация</a>
</li>
<% end %>

</ul>
</div>
</div>
Expand Down
26 changes: 25 additions & 1 deletion app/views/posts/_post.html.erb
Expand Up @@ -11,5 +11,29 @@
<a href="<%= url_for(profile_path(id: post.user.id)) %>"><%= post.user.email %></a>
on <%= post.created_at.strftime('%d-%m-%Y %H:%M') %>
</p>

<div class="btn-group">
<% if current_user != nil and current_user.id == post.user.id %>
<a class="btn btn-sm btn-info" href="<%= url_for(edit_post_path(post)) %>" role="button">Редактировать</a>
<a class="btn btn-sm btn-danger" roloe="button" data-toggle="modal" data-target="#post-delete-<%=post.id%>" href="#">Удалить</a>
<% end %>
</div>

</div>
<hr>
<hr>

<% if current_user != nil and current_user.id == post.user.id %>
<div id="post-delete-<%= post.id %>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenter">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="card-header">
<div class="modal-body text-center">Вы уверены что хотите это удалить?</div>
<div class="d-flex justify-content-between align-items-center">
<%= button_to "Удалить", post, { method: :delete, class: 'btn btn-danger' } %>
<a class="btn btn-secondary" type="button" data-dismiss="modal">Отмена</a>
</div>
</div>
</div>
</div>
</div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/posts/edit.html.erb
@@ -0,0 +1,9 @@
<% content_for :h1 do %>
Редактирование новой статьи
<% end %>
<% content_for :title do %>
Редактирование новой статьи
<% end %>
<%= render 'form', post: @post %>

0 comments on commit 24ddcb4

Please sign in to comment.