0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

HTMLの基礎知識(コメント機能で動画を投稿)

Posted at

みなさんこんにちは!!
今回はYouTude投稿とコメント機能の応用した機能になります!!


STEP1:モデルを作成しよう!

ターミナル
rails g model Youtubecomment content:string user:references register:references
rails generate migration AddYoutubeUrlToRegisters youtube_url:string
rails db:migrate

Step4: helperにメソッドの定義

helpers/registersHelper
module RegistersHelper
  def find_youtube_url(youtube_url)
    if youtube_url[0..16] == "https://youtu.be/"
      return youtube_url[17..27]
      # "https://youtu.be/WGiUk8VakxQ" 11桁のyoutubeのURLが出力されるようにする
    else
      return youtube_url[32..42]
      # "https://www.youtube.com/watch?v=WGiUk8VakxQ" 11桁のyoutubeのURLが出力されるようにする
    end
  end
end

STEP2:アソシエーションを組み

app/models/user.rb
has_many :youtubecomments, dependent: :destroy
app/models/register.rb
has_many :youtubecomments, dependent: :destroy

STEP3:コントローラーの作成

ターミナル
rails g controller youtubecomments

STEP4:コントローラーの編集

app/controllers/youtubecomments_controller.rb
class YoutubecommentsController < ApplicationController
  before_action :authenticate_user!

  def create
   @register = Register.find(params[:register_id])
    youtubecomment = @register.youtubecomments.build(youtubecomment_params) #buildを使い、contentとtweet_idの二つを同時に代入
    youtubecomment.user_id = current_user.id
    if youtubecomment.save
      flash[:success] = "コメントしました"
      redirect_back(fallback_location: root_path)
    else
      flash[:success] = "コメントできませんでした"
      redirect_back(fallback_location: root_path)
    end
  end

  private

    def youtubecomment_params
      params.require(:youtubecomment).permit(:content)
    end
end
app/controllers/registers_controller.rb
# 省略
def show
        @register = Register.find(params[:id])
# 追記ここから
        @youtubecomments = @register.youtubecomments
        @youtubecomment = Youtubecomment.new
# 追記ここまで
    end
#省略
private
    def register_params
        params.require(:register).permit(:youtube_url)
    end

STEP5:ルーティング

routes.rb
#追記箇所
    resources :registers do
   #追加
    resources :youtubecomments, only: [:create] 
   # ここまで
    end

STEP6:Viewsの設定

app/views/registers/show.html.erb
# 追記ここから
<div class="comment-wrapper">
  <p>コメント一覧</p>
  <% @youtubecomments.each do |c| %>
    <div>
      <%= c.user.email unless c.user.blank? %>
      <br>
      <%= c.content %>
      <br>
       <iframe width="560" height="315" src="https://www.youtube.com/embed/<%= find_youtube_url(@register.youtube_url) %>" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
      <br>
  <% end %>

  <% if user_signed_in? %>
    <%= form_with(model: [@register, @youtubecomment], local: true) do |f| %>
      <%= f.text_area :content %>
      <%= button_tag type: "submit" do %>
        <i class="far fa-comments"></i> コメントする
      <% end %>
    <% end %>
  <% end %>
</div>
#追記ここまで

以上です!!
少し長かったですね!
お疲れ様でした!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?