File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,33 +22,13 @@ def edit
2222 # POST /posts or /posts.json
2323 def create
2424 @post = Post . new ( post_params )
25-
26- if params [ :publish ]
27- if @post . publish
28- redirect_to @post , notice : "Post published"
29- else
30- render :new , status : :unprocessable_entity
31- end
32- else
33- @post . save
34- redirect_to @post , notice : "Draft saved"
35- end
25+ save_post ( :new )
3626 end
3727
3828 # PATCH/PUT /posts/1 or /posts/1.json
3929 def update
4030 @post . assign_attributes ( post_params )
41-
42- if params [ :publish ]
43- if @post . publish
44- redirect_to @post , notice : "Post published"
45- else
46- render :edit , status : :unprocessable_entity
47- end
48- else
49- @post . save
50- redirect_to @post , notice : "Draft updated"
51- end
31+ save_post ( :edit )
5232 end
5333
5434 # DELETE /posts/1 or /posts/1.json
@@ -68,4 +48,25 @@ def set_post
6848 def post_params
6949 params . expect ( post : [ :title , :body ] )
7050 end
51+
52+ def save_post ( template )
53+ if params [ :publish ]
54+ publish_post ( template )
55+ else
56+ save_draft
57+ end
58+ end
59+
60+ def publish_post ( template )
61+ if @post . publish
62+ redirect_to @post , notice : "Post published"
63+ else
64+ render template , status : :unprocessable_entity
65+ end
66+ end
67+
68+ def save_draft
69+ @post . save
70+ redirect_to @post , notice : "Draft saved"
71+ end
7172end
Original file line number Diff line number Diff line change @@ -2,6 +2,6 @@ class Post < ApplicationRecord
22 validates :title , :body , presence : true , on : :publish
33
44 def publish
5- save if valid? ( :publish )
5+ save ( context : :publish )
66 end
77end
You can’t perform that action at this time.
0 commit comments