Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. Routes:
  2.  
  3. Rails.application.routes.draw do
  4.  
  5. root "posts#index"
  6.  
  7. resources :posts do
  8. resources :comments
  9. end
  10.  
  11. end
  12.  
  13.  
  14.  
  15. Post Controller:
  16.  
  17. class PostsController < ApplicationController
  18.  
  19.  
  20. def index
  21. @posts = Post.all
  22.  
  23. if @posts.blank?
  24. flash[:alert] = "No posts have been created."
  25. else
  26. @posts
  27. end
  28. end
  29.  
  30. def edit
  31. @post = Post.find(params[:id])
  32. end
  33.  
  34. def update
  35. @post = Post.find(params[:id])
  36.  
  37. if @post.update(post_params)
  38. flash[:notice] = "Post has been updated."
  39. redirect_to @post
  40. else
  41. flash.now[:alert] = "Post has not been updated."
  42. render "edit"
  43. end
  44. end
  45.  
  46. def create
  47. @post = Post.new(post_params)
  48.  
  49. if @post.save
  50. flash[:notice] = "Post has been created."
  51. redirect_to @post
  52. else
  53. flash[:notice] = "Post has not been created."
  54. end
  55. end
  56.  
  57. def new
  58. @post = Post.new
  59. end
  60.  
  61. def show
  62. @post = Post.find(params[:id])
  63. @comment = @post.comments.find(params[:id])
  64. end
  65.  
  66. def destroy
  67. @post = Post.find(params[:id])
  68. @post.destroy
  69.  
  70. flash[:notice] = "Post has been deleted."
  71. redirect_to posts_path
  72. end
  73.  
  74.  
  75. private
  76.  
  77. def post_params
  78. params.require(:post).permit(:title, :author, :body)
  79. end
  80.  
  81. end
  82.  
  83.  
  84.  
  85.  
  86. Comments Controller:
  87.  
  88. class CommentsController < ApplicationController
  89.  
  90.  
  91. def create
  92. @post = Post.find(params[:post_id])
  93. @comment = @post.comments.create(comment_params)
  94. redirect_to post_path(@post)
  95. end
  96.  
  97.  
  98. private
  99.  
  100. def comment_params
  101. params.require(:comment).permit(:author, :body)
  102. end
  103. end
  104.  
  105.  
  106. Comments View Files
  107.  
  108. _comment.html.erb
  109.  
  110. <p>
  111. Author:
  112. <%= comment.author %>
  113. </p>
  114. <p>
  115. Body:
  116. <%= comment.body %>
  117. </p>
  118.  
  119.  
  120. _form.html.erb
  121.  
  122. <%= form_for([@post, @post.comments.build]) do |f| %>
  123. <p>
  124. <%= f.label :author %><br />
  125. <%= f.text_field :author %>
  126. </p>
  127. <p>
  128. <%= f.label :body %><br />
  129. <%= f.text_area :body %>
  130. </p>
  131. <p>
  132. <%= f.submit %>
  133. </p>
  134. <% end %>
  135.  
  136.  
  137.  
  138. Post View Files
  139.  
  140. _form.html.erb
  141.  
  142. <%= form_for @post do |f| %>
  143. <p>
  144. <%= f.label :title %><br />
  145. <%= f.text_field :title %>
  146. </p>
  147.  
  148. <p>
  149. <%= f.label :author %><br />
  150. <%= f.text_field :author %>
  151. </p>
  152.  
  153. <p>
  154.  
  155. edit.html.erb
  156.  
  157. <h3>Edit Post</h3>
  158.  
  159. <%= render 'form' %>
  160.  
  161. index.html.erb
  162.  
  163. <h1>Overall Posts</h1>
  164.  
  165. <% @posts.each do |post| %>
  166. <p>
  167. <b>Title:</b>
  168. <%= post.title %>
  169. </p>
  170. <p>
  171. <b>Author:</b>
  172. <%= post.author %>
  173. </p>
  174. <p>
  175. <b>Body</b>
  176. <%= post.body %>
  177. </p>
  178. <p>
  179. <%= link_to 'Show', post_path(post) %> |
  180. <%= link_to 'Edit', edit_post_path(post) %> |
  181. <%= link_to 'Destroy', post_path(post), method: :delete, data: { confirm: 'Are you sure?' } %>
  182. </p>
  183. <% end %>
  184.  
  185. <p><%= link_to "Create New Post", new_post_path %>
  186.  
  187.  
  188. new.html.erb
  189.  
  190. <h3>Create New Post</h3>
  191.  
  192. <%= render 'form' %>
  193.  
  194.  
  195. <p><%= link_to "Posts Index", posts_path %></p>
  196.  
  197.  
  198. show.html.erb
  199.  
  200. <p><%= link_to "Posts Index", posts_path %></p>
  201.  
  202. <h3>Post's details</h3>
  203.  
  204. <h3>Title</h3>
  205. <%= @post.title %>
  206.  
  207. <h3>Author</h3>
  208. <%= @post.author %>
  209.  
  210. <h3>Body</h3>
  211. <%= @post.body %>
  212.  
  213. <h2>Recent Comments</h2>
  214.  
  215. <%= render @comments.comments %>
  216.  
  217.  
  218. <h2>Add Comments</h2>
  219.  
  220. <%= render 'comments/form' %>
  221. <%= f.label :body %><br />
  222. <%= f.text_area :body %>
  223. </p>
  224.  
  225. <p>
  226. <%= f.submit %>
  227. </p>
  228.  
  229. <% end %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement