Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ToppagesController < ApplicationController
- def index
- if logged_in?
- @user = current_user
- @post = current_user.posts.build # form_for 用
- @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
- @comments = @post.comments.order('created_at DESC').page(params[:page])
- end
- end
- end
- class CommentsController < ApplicationController
- #before_action :set_comment, only: [:new,:create, :destroy]
- before_action :require_user_logged_in
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.build(comment_params)
- @comment.user_id = current_user.id
- #@comment = current_user.posts.comments.build(comment_params)
- #@comment = Comment.create(text: comment_params[:text], post_id: comment_params[:post_id], user_id: current_user.id)
- if @comment.save
- flash[:success] = "コメントしました。"
- #redirect_to "/posts/#{@comment.post.id}"
- #redirect_to post_comments_path(@post.id)
- #redirect_to :action =>"new"
- redirect_to root_url
- else
- @comments = @post.comments.order('created_at DESC').page(params[:page])
- flash.now[:danger] = 'コメントの投稿に失敗しました。'
- render 'toppages/index'
- end
- end
- def destroy
- @comment.destroy
- flash[:success] = 'コメントを削除しました。'
- redirect_back(fallback_location: root_path)
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_comment
- @post = Post.find(params[:post_id])
- @comment = @post.comments.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def comment_params
- params.require(:comment).permit(:user_id, :post_id, :content)
- end
- end
- class PostsController < ApplicationController
- before_action :require_user_logged_in
- before_action :correct_user, only: [:destroy]
- def index
- @comment = @post.comments.build(comment_params)
- end
- def show
- @post = Post.includes(:user).find(params[:id])
- end
- def create
- @post = current_user.posts.build(post_params)
- if @post.save
- flash[:success] = 'メッセージを投稿しました。'
- redirect_to root_url
- else
- @posts = current_user.feed_posts.order('created_at DESC').page(params[:page])
- flash.now[:danger] = 'メッセージの投稿に失敗しました。'
- render 'toppages/index'
- end
- end
- def destroy
- @post.destroy
- flash[:success] = 'メッセージを削除しました。'
- redirect_back(fallback_location: root_path)
- end
- private
- def post_params
- params.require(:post).permit(:picture, :content)
- end
- def correct_user
- @post = current_user.posts.find_by(id: params[:id])
- unless @post
- redirect_to root_url
- end
- end
- end
- <ul class="media-list">
- <% @comments.each do |comment| %>
- <div class="name2">投稿者:<%= link_to comment.user, "/users/#{comment.user_id}" %> 投稿日時:<%= comment.created_at.strftime("%Y-%m-%d %H:%M:%S") %></div>
- <div class="name2"><%= comment.content %></div>
- <div>
- <% if current_user == comment.user %>
- <%= link_to "削除", comment, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
- <% end %>
- </div>
- <% end %>
- <%= paginate comments %>
- <ul class="media-list">
- <% posts.each do |post| %>
- <% user = post.user %>
- <li class="media">
- <div class="media-left">
- <img class="media-object img-rounded" src="<%= gravatar_url(user, options = { size: 50 }) %>" alt="">
- </div>
- <div class="media-body">
- <div>
- <%= link_to user.name, user_path(user) %> <span class="text-muted">posted at <%= post.created_at %></span>
- </div>
- <div>
- <p><%= image_tag post.picture,:size =>"280x210" %></p>
- <p><%= post.content %></p>
- <%= render 'comments/comments', comments: @comments %>
- <br/>
- <% if current_user %>
- <%= form_for [post, Comment.new] do |form| %>
- <%= form.text_area :content, cols: "30", placeholder: "コメントする", rows: "2" %>
- <%= form.submit "コメントの投稿" %>
- <% end %>
- <% end %>
- </div>
- <div>
- <% if current_user == post.user %>
- <%= link_to "削除", post, method: :delete, data: { confirm: "本当に削除してよろしいですか?" }, class: 'btn btn-danger btn-sm' %>
- <% end %>
- </div>
- </div>
- </li>
- <% end %>
- <%= paginate posts %>
- </ul>
- <% content_for :cover do %>
- <% if logged_in? %>
- <div class="row">
- <aside class="col-md-4">
- <%= form_for(@post, html: {multipart: true}) do |f| %>
- <div class="form-group">
- <%= f.label :picture %>
- <%= f.file_field :picture %><br />
- <%= f.text_area :content, class: 'form-control', rows: 5 %>
- </div>
- <%= f.submit '投稿', class: 'btn btn-primary btn-md' %>
- <% end %>
- </aside>
- <div class="col-xs-8">
- <%= render 'posts/posts', posts: @posts %>
- </div>
- </div>
- <% else %>
- <div class="cover">
- <div class="cover-inner">
- <div class="cover-contents">
- <h1>毎日のつながりは、ここから始まる</h1>>
- <%= link_to 'まずは会員登録から', signup_path, class: 'btn btn-success btn-md' %>
- <%= link_to '会員の方はこちら', login_path, class: 'btn btn-success btn-md' %>
- </div>
- </div>
- </div>
- <% end %>
- <% end %>
- @comment = @post.comments.build
- @comments = @post.comments.order('created_at DESC').page(params[:page])
Add Comment
Please, Sign In to add comment