Actioncable + devise でリアルタイムチャットアプリを作成しています。
チャット画面で投稿する際に以下のエラーがでてしまい、画面リロードをしないと文字が表示されなくなりました。
Finished "/cable/" [WebSocket] for 192.168.33.1 at 2019-11-29 04:22:12 +0100
RoomChannel stopped streaming from room_channel_
Started GET "/cable" for 192.168.33.1 at 2019-11-29 04:22:12 +0100
Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Started GET "/cable/" [WebSocket] for 192.168.33.1 at 2019-11-29 04:22:12 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/channels/application_cable/connection.rb:12:in find_verified_user' User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/channels/application_cable/connection.rb:12:in
find_verified_user'
Registered connection (Z2lkOi8vY2hhdGFwcC9Vc2VyLzE)
RoomChannel is transmitting the subscription confirmation
RoomChannel is streaming from room_channel_1
RoomChannel#speak({"message"=>"i have no idea"})
(0.1ms) begin transaction
↳ app/channels/room_channel.rb:12:in speak' User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/channels/room_channel.rb:12:in
speak'
Room Load (0.1ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/channels/room_channel.rb:12:in speak' Message Create (2.8ms) INSERT INTO "messages" ("content", "created_at", "updated_at", "user_id", "room_id") VALUES (?, ?, ?, ?, ?) [["content", "i have no idea"], ["created_at", "2019-11-29 03:22:23.549954"], ["updated_at", "2019-11-29 03:22:23.549954"], ["user_id", 1], ["room_id", 1]] ↳ app/channels/room_channel.rb:12:in
speak'
(7.4ms) commit transaction
↳ app/channels/room_channel.rb:12:in speak' [ActiveJob] Enqueued MessageBroadcastJob (Job ID: 57f40449-6e0e-41c7-8dbc-efd67e142aac) to Async(default) with arguments: #<GlobalID:0x00007f2910d615b0 @uri=#<URI::GID gid://chatapp/Message/59>> Message Load (0.2ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 59], ["LIMIT", 1]] [ActiveJob] [MessageBroadcastJob] [57f40449-6e0e-41c7-8dbc-efd67e142aac] Performing MessageBroadcastJob (Job ID: 57f40449-6e0e-41c7-8dbc-efd67e142aac) from Async(default) enqueued at 2019-11-29T03:22:23Z with arguments: #<GlobalID:0x00007f2928034708 @uri=#<URI::GID gid://chatapp/Message/59>> [ActiveJob] [MessageBroadcastJob] [57f40449-6e0e-41c7-8dbc-efd67e142aac] Rendered messages/_message.html.erb (Duration: 6.9ms | Allocations: 1487) [ActiveJob] [MessageBroadcastJob] [57f40449-6e0e-41c7-8dbc-efd67e142aac] Error performing MessageBroadcastJob (Job ID: 57f40449-6e0e-41c7-8dbc-efd67e142aac) from Async(default) in 8.56ms: ActionView::Template::Error (undefined method
image' for nil:NilClass):
/home/vagrant/environment/chatapp/app/views/messages/_message.html.erb:2:in _app_views_messages__message_html_erb__1902079675368871319_69907370882620' /home/vagrant/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/actionview-6.0.1/lib/action_view/base.rb:274:in
_run'
ネットで調べ、controllerを変更したり、deviseの仕様についても調べましたが,解決しませんでした。
以下が関係していると感じているコードです。
rooms_controller.rb ↓
rooms_controller.rb
1 2class RoomsController < ApplicationController 3 4 def index 5 @user = current_user.id 6 @rooms = Room.all.order(:id) 7 end 8 9 def show 10 @user = User.find(params[:id]) 11 @room = Room.find(params[:id]) 12 @messages = @room.messages 13 end 14end 15 16</code> 17 18コード
routes.rb ↓
Rails.application.routes.draw
1 namespace :admin do 2 resources :users 3 end 4 5 devise_for :users, :controllers => { 6 registrations: 'users/registrations'} 7 8 9 root to: 'top#show' 10 11 resources :rooms, :only => [:index, :show] 12 13 devise_scope :user do 14 get 'users/sign_out', :to => 'users/sessions#destroy' 15 end 16 17 resources :users, :only => [:index, :show, :edit, :destroy] 18 19end 20コード
users_controller.rb ↓
class
1 before_action :authenticate_user! 2 3 def show 4 @user = current_user.id 5 end 6 7 def index 8 @user = User.find(params[:id]) 9 end 10 11 def edit 12 @user = current_user.id 13 end 14 15 def update 16 end 17 18 def destroy 19 @user = User.find(params[:id]) 20 if @user.destroy 21 redirect_to admin_users_url 22 flash[:notice] = "ユーザー#{@user.username}を削除しました。" 23 end 24 end 25 26end 27コード
rooms show.html.erb ↓
<h1>Chat room</h1> <div id='messages' data-room_id="<%= @room.id %>"> <%= render @messages %> </div> </div> <div class="send-box"> <%= text_field_tag :content, nil, data: { behavior: 'room_speaker' } %> <%= label_tag :content, 'Say something' %> </div> コード
_messages.erb ↓
<% if @user.image.present? %> <%= image_tag(@user.image.thumb100.url) %> <% else %> <%= image_tag('no-image-icon-23500.jpg', :size => '100x100') %> <% end %> <p><%= "#{message.user.email}: #{message.content}" %></p> </div> コード
schema.rb ↓
create_table
1 t.string "email", default: "", null: false 2 t.string "encrypted_password", default: "", null: false 3 t.string "reset_password_token" 4 t.datetime "reset_password_sent_at" 5 t.datetime "remember_created_at" 6 t.datetime "created_at", precision: 6, null: false 7 t.datetime "updated_at", precision: 6, null: false 8 t.string "username" 9 t.boolean "admin", default: false, null: false 10 t.text "profile" 11 t.string "image" 12 t.index ["email"], name: "index_users_on_email", unique: true 13 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 14 end 15 16コード
rooms index.html.erb ↓
<h1><%= link_to "#{current_user.username}さんのページ", user_path(@user) %></h1> <h3>Chatroom一覧</h3> <div> <ul> <% @rooms.each do |room| %> <li><%= link_to "ROOM#{room.id}", room_path(room.id, @user.id) %></li> <% end %> </ul> </div> <h3>お気に入り</h3> <% if current_user.try(:admin?) %> <%= link_to "管理者画面", admin_users_path, class: 'btn' %> <% end %> </div> コード
schemaファイルにはimageカラムがあるのに何故エラーが出ているのでしょうか?
ご助力お願いします。
回答1件
あなたの回答
tips
プレビュー