Guest User

Untitled

a guest
Feb 1st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. class ToppagesController < ApplicationController
  2. def index
  3. if logged_in?
  4. @user = current_user
  5. @post = current_user.posts.build # form_for 用
  6. @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
  7. @comments = @post.comments.order('created_at DESC').page(params[:page])
  8. end
  9. end
  10. end
  11.  
  12. class CommentsController < ApplicationController
  13. #before_action :set_comment, only: [:new,:create, :destroy]
  14. before_action :require_user_logged_in
  15.  
  16. def create
  17. @post = Post.find(params[:post_id])
  18. @comment = @post.comments.build(comment_params)
  19. @comment.user_id = current_user.id
  20. #@comment = current_user.posts.comments.build(comment_params)
  21. #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
  22. if @comment.save
  23. flash[:success] = "コメントしました。"
  24. #redirect_to "/posts/#{@comment.post.id}"
  25. #redirect_to post_comments_path(@post.id)
  26. #redirect_to :action =>"new"
  27. redirect_to root_url
  28. else
  29. @comments = @post.comments.order('created_at DESC').page(params[:page])
  30. flash.now[:danger] = 'コメントの投稿に失敗しました。'
  31. render 'toppages/index'
  32. end
  33. end
  34.  
  35. def destroy
  36. @comment.destroy
  37. flash[:success] = 'コメントを削除しました。'
  38. redirect_back(fallback_location: root_path)
  39. end
  40.  
  41. private
  42. # Use callbacks to share common setup or constraints between actions.
  43. def set_comment
  44. @post = Post.find(params[:post_id])
  45. @comment = @post.comments.find(params[:id])
  46. end
  47.  
  48. # Never trust parameters from the scary internet, only allow the white list through.
  49. def comment_params
  50. params.require(:comment).permit(:user_id, :post_id, :content)
  51. end
  52. end
  53.  
  54. class PostsController < ApplicationController
  55. before_action :require_user_logged_in
  56. before_action :correct_user, only: [:destroy]
  57.  
  58. def index
  59. @comment = @post.comments.build(comment_params)
  60. end
  61.  
  62. def show
  63. @post = Post.includes(:user).find(params[:id])
  64. end
  65.  
  66. def create
  67. @post = current_user.posts.build(post_params)
  68. if @post.save
  69. flash[:success] = 'メッセージを投稿しました。'
  70. redirect_to root_url
  71. else
  72. @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
  73. flash.now[:danger] = 'メッセージの投稿に失敗しました。'
  74. render 'toppages/index'
  75. end
  76. end
  77.  
  78. def destroy
  79. @post.destroy
  80. flash[:success] = 'メッセージを削除しました。'
  81. redirect_back(fallback_location: root_path)
  82. end
  83.  
  84. private
  85.  
  86. def post_params
  87. params.require(:post).permit(:picture, :content)
  88. end
  89.  
  90. def correct_user
  91. @post = current_user.posts.find_by(id: params[:id])
  92. unless @post
  93. redirect_to root_url
  94. end
  95. end
  96. end
  97.  
  98. <ul class="media-list">
  99. <% @comments.each do |comment| %>
  100. <div class="name2">投稿者:<%= link_to comment.user, "/users/#{comment.user_id}" %>&nbsp;&nbsp;投稿日時:<%= comment.created_at.strftime("%Y-%m-%d %H:%M:%S") %></div>
  101. <div class="name2"><%= comment.content %></div>
  102. <div>
  103. <% if current_user == comment.user %>
  104. <%= link_to "削除", comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
  105. <% end %>
  106. </div>
  107. <% end %>
  108. <%= paginate comments %>
  109.  
  110. <ul class="media-list">
  111. <% posts.each do |post| %>
  112. <% user = post.user %>
  113. <li class="media">
  114. <div class="media-left">
  115. <img class="media-object img-rounded" src="<%= gravatar_url(user, options = { size: 50 }) %>" alt="">
  116. </div>
  117. <div class="media-body">
  118. <div>
  119. <%= link_to user.name, user_path(user) %> <span class="text-muted">posted at <%= post.created_at %></span>
  120. </div>
  121. <div>
  122. <p><%= image_tag post.picture,:size =>"280x210" %></p>
  123. <p><%= post.content %></p>
  124. <%= render 'comments/comments', comments: @comments %>
  125. <br/>
  126. <% if current_user %>
  127. <%= form_for [post, Comment.new] do |form| %>
  128. <%= form.text_area :content, cols: "30", placeholder: "コメントする", rows: "2" %>
  129. <%= form.submit "コメントの投稿" %>
  130. <% end %>
  131. <% end %>
  132. </div>
  133. <div>
  134. <% if current_user == post.user %>
  135. <%= link_to "削除", post, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
  136. <% end %>
  137. </div>
  138. </div>
  139. </li>
  140. <% end %>
  141. <%= paginate posts %>
  142. </ul>
  143.  
  144. <% content_for :cover do %>
  145. <% if logged_in? %>
  146. <div class="row">
  147. <aside class="col-md-4">
  148. <%= form_for(@post, html: {multipart: true}) do |f| %>
  149. <div class="form-group">
  150. <%= f.label :picture %>
  151. <%= f.file_field :picture %><br />
  152. <%= f.text_area :content, class: 'form-control', rows: 5 %>
  153. </div>
  154. <%= f.submit '投稿', class: 'btn btn-primary btn-md' %>
  155. <% end %>
  156. </aside>
  157. <div class="col-xs-8">
  158. <%= render 'posts/posts', posts: @posts %>
  159. </div>
  160. </div>
  161. <% else %>
  162. <div class="cover">
  163. <div class="cover-inner">
  164. <div class="cover-contents">
  165. <h1>毎日のつながりは、ここから始まる</h1>>
  166. <%= link_to 'まずは会員登録から', signup_path, class: 'btn btn-success btn-md' %>
  167. <%= link_to '会員の方はこちら', login_path, class: 'btn btn-success btn-md' %>
  168. </div>
  169. </div>
  170. </div>
  171. <% end %>
  172. <% end %>
  173.  
  174. @comment = @post.comments.build
  175. @comments = @post.comments.order('created_at DESC').page(params[:page])
Add Comment
Please, Sign In to add comment