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 DM機能その2(DM投稿画面)

0
Posted at

Rails DM機能その1の続きです。

開発環境

Mac
Ruby 2.6.5
Rails 6.1.3.2

rooms/show.html.erb

<%= form_for @message do |f| %>
<%= f.text_field :content, :placeholder => "メッセージを入力して下さい" , :size => 70 %>
<%= f.hidden_field :room_id, :value => @room.id %>
<%= f.submit "投稿する" %>
<% end %>

コードの解説

フォームに投稿された内容をテーブルに保存したいのでform_forを使います。
form系ヘルパーメソッドの使い分け
ユーザーが入力したメッセージの内容を取得したいため、f.text_field :contentの記述が必要です。
どのチャットルームのメッセージが判断するために、f.hidden_fieldで:room_idのバリューとして、チャットルームでのidを取得しています。
f.hidden_fieldとhidden_field_tagの使い方【Ruby on Rails】
f.submitとすることで、投稿した時に、messages_controllerにパラメータが飛ぶようになっています。

参考記事

RailsでややこしいDM機能を1万字でくわしく解説してみた
RailsでDM(ダイレクトメッセージ)を送れるようにしよう

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?