Tworzenie Prostej Platformy Blogerskiej - Usuwanie Postów
Przez: Lukasz Muzyka, Z dnia:
Ten tutorial zakłada, że już ukończyłeś:
- Install Ruby on Rails
- Create Ruby on Rails application
- Create Static Pages - without this deploy will not work
- Install Git
- Create Remote Git Repository - optional but recommended
- Deploy application to Heroku
- Manage users with Devise
- How to add Twitter Bootstrap to Ruby on Rails application - Advised
- Creating Simple Blogging Platform - Creating Model
- Creating Simple Blogging Platform - Routes
- Creating Simple Blogging Platform - Displaying Posts
- Creating Simple Blogging Platform - Building Posts
- Creating Simple Blogging Platform - Editing Posts
Ostanią rzeczą, na którą pozwolimy naszym użytkownikom, będzie usuwanie postów. Na szczęście, zważywszy na prace, którą do tej pory wykonaliśmy, będzie to łatwe zadanie.
Krok 1: Stwórz Akcję w Kontrolerze
Tak samo jak przedtem, zaczniemy od stworzenie akcji w kontrolerze:
/app/controllers/posts_controller.rb
class PostsController < ApplicationController
# existing code
def destroy
@post = Post.find(params[:id])
if @post.user == current_user
@post.destroy
redirect_to posts_path
else
redirect_to root_path
flash[:danger] = "You can't do this"
end
end
private
def allowed_params
params.require(:post).permit(:title, :body)
end
end
Tak jak to zrobiliśmy przedtem, musimy znaleźć post w bazie danych. Do wyszukiwania użyjemy :id
z URL. Następnie, sprawdzimy czy post należy do użytkownika, który chce go usunąć. Jeżeli nie, przekierujemy go na stronę domową z wiadomośćią o błędzie, inaczej, usuniemy rekord i zabierzemy użytkownika do listy postów.
Krok 2: Dodaj link
Teraz musimy dodać link, który będzie wywoływał akcję "destroy". Uzytkownik prawdopodobnie będzie oczekiwał linku "delete" na stronie edycji posta:
app/views/posts/edit.html.erb
<h1>Edit post</h1>
<p><%= link_to 'back', post_path(@post) %></p>
<%= render 'form', post: @post %>
<br />
<p><%= link_to 'delete post', @post, method: :delete, data: {confirm: "Are you sure you want to delete this permanently?"} %></p>
Krok 3: Trochę refaktoryzacji
Kiedy patrzymy na nasz kontroler możemy łatwo zauważyć, że jest tak trochę kodu, który się powtarza. Zmieńmy to.
Najpierw, wyciągnijmy część odpowiadającą za znajdowanie posta z params[:id]
i dodajmy to jako before_filter
dla tych akcji:
/app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
before_filter :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
# @post = Post.find(params[:id])## NOT NEEDED ANYMORE MOVED TO BEFORE FILTER
end
def new
@post = Post.new
end
def create
@user = current_user
@post = @user.posts.build(allowed_params)
if @post.save
flash[:success] = "Created new post"
redirect_to @post
else
render 'new'
end
end
def edit
# @post = Post.find(params[:id])## NOT NEEDED ANYMORE MOVED TO BEFORE FILTER
redirect_to posts_path unless @post.user == current_user
end
def update
# @post = Post.find(params[:id])## NOT NEEDED ANYMORE MOVED TO BEFORE FILTER
if @post.user == current_user
if @post.update_attributes(allowed_params)
flash[:success] = "Updated post"
redirect_to @post
else
render 'edit'
end
else
redirect_to posts_path
flash[:notice] = "You can't do this"
end
end
def destroy
# @post = Post.find(params[:id])## NOT NEEDED ANYMORE MOVED TO BEFORE FILTER
if @post.user == current_user
@post.destroy
redirect_to posts_path
flash[:success] = "deleted post"
else
redirect_to root_path
flash[:danger] = "You can't do this"
end
end
private
def allowed_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
end
Możemy zmienić nieco więcej, ale na razie tyle wystarczy. Końcowa wersja kontrolera wygląda tak:
/app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
before_filter :find_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def new
@post = Post.new
end
def create
@user = current_user
@post = @user.posts.build(allowed_params)
if @post.save
flash[:success] = "Created new post"
redirect_to @post
else
render 'new'
end
end
def edit
redirect_to posts_path unless @post.user == current_user
end
def update
if @post.user == current_user
if @post.update_attributes(allowed_params)
flash[:success] = "Updated post"
redirect_to @post
else
render 'edit'
end
else
redirect_to posts_path
flash[:notice] = "You can't do this"
end
end
def destroy
if @post.user == current_user
@post.destroy
redirect_to posts_path
flash[:success] = "deleted post"
else
redirect_to root_path
flash[:danger] = "You can't do this"
end
end
private
def allowed_params
params.require(:post).permit(:title, :body)
end
def find_post
@post = Post.find(params[:id])
end
end
Dodaj komentarz
Możesz się zalogować by skomentować