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 3 years have passed since last update.

railsで超シンプルなコメント機能を作った時の手順

Last updated at Posted at 2019-12-04

rails g controller comment
rails g model Comment post_id:integer comment:string
rails db:migrate

ルート
post "comments/create/:id" => "comment#create" # :idはcommentテーブルのpost_id参照に使う

コントローラ
def create
@comment = Comment.new(post_id: params[:id],
comment: params[:comment])
@comment.save
redirect_to "/"
end

ビュー
formforを取り付けたいビューに張り付け
@comment = Comment.new()をformforに連動するアクションに張り付け

posts/showの下部にコメント入力と送信、表示を取り付ける。
最初はコメント入力と送信

formtagに変更

<%= form_tag("/comments/create/#{@a.id}") do %>

  <textarea name="comment"></textarea>
  <input type="submit" value="コメント投稿" %>

<% end %></br>
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?