前提・実現したいこと
現在、DM機能の実装をしている初心者なのですが、構文エラーが出てしまい躓いています。どなたかご教授いただければ幸いです。
該当のソースコード
users/show.html.erb
ruby
1<% unless current_user.id == @user.id %> 2 <% if @isRoom == true %> 3 <%= link_to "メッセージを送る", room_path, class="chat-start-red-btn" %> 4 <% else %> 5 <%= form_with model: @room, lpcal: true do |f| %> 6 <%= fields_for @entry do |e| %> 7 <%= e.hidden_field :user_id, value: @user.id %> 8 <% end %> 9 <%= f.submit "メッセージを送る", class="chat-start-red-btn" %> 10 <% end %> 11 <% end %> 12 <% end %>
users_controller.rb
ruby
1def show 2 @user = User.find(params[:id]) 3 @currentUserEntry = Entry.where(user_id: current_user.id) 4 @userEntry = Entry.where(user_id: @user.id) 5 6 unless @user.id == current_user.id 7 @currentUserEntry.each do |cu| 8 @userEntry.each do |u| 9 if cu.room_id == u.room_id 10 @isRoom = true 11 @roomId = cu.room_id 12 end 13 end 14 end 15 unless @isRoom 16 @room = Room.new 17 @entry = Entry.new 18 end 19 end 20 end
rooms_controller.rb
ruby
1def create 2 @room = Room.create 3 @entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) 4 @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id)) 5 redirect_to room_path(@room.id) 6 end 7 8 def show 9 @room = Room.find(params[:id]) 10 if Entry.where(user_id: current_user.id, room_id: @room.id).present? 11 @messages = @room.messages 12 @message = Message.new 13 @entries = @room.entries 14 else 15 redirect_back(fallback_location: root_path) 16 end 17 end
routes.rb
ruby
1Rails.application.routes.draw do 2 devise_for :users 3 root to: "users#index" 4 resources :users 5 resources :messages, only: [:create] 6 resources :rooms, only: [:create, :show] 7end
自分で調べたことや試したこと
おそらくルームを作成する際のパラメータ情報がうまく送られていないと思われるのですが、自分では解決まで辿り着けませんでしたので、お力をお貸しいただければ幸いです。
使っているツールのバージョンなど補足情報
Rails 6.0.0
Ruby 2.6.5
回答1件
あなたの回答
tips
プレビュー