Home>

This pageI implemented the message function with reference to.
I was able to send and receive messages without any problems, so I was messing around with CSS with confidence, but before I knew it, an error began to appear when sending messages.

undefined local variable or method,redirect_back (fallback_location: root_path)Because it is an error about, misspelling or forgetting to define, "The point that redirect_to: back cannot be used from Rails 5.1 seriesI confirmed, but none of them seem to be applicable.

NameError in MessagesController # create
undefined local variable or method `root_path'for #<MessagesController: 0x00000000135e39f0>Did you mean? room_path rooms_path logout_path post_path
Extracted source (around line # 16):
    else else
      redirect_back (fallback_location: root_path)
    end
  end
end
messages_controller.rb
class MessagesController  @ current_user.id,: room_id =>params [: message] [: room_id]). present?
      @message = Message.create (params.require (: message) .permit (: user_id,: content,: room_id) .merge (: user_id =>@ current_user.id))
      redirect_to "/rooms/#{@message.room_id}"
    else else
      redirect_back (fallback_location: root_path)
    end
  end
end
room.show.html.erb
          <% = form_for @message do | f |%>      <% = f.text_field: content,: placeholder =>"Write a message…",: size =>70%>      <% = f.submit "Send"%>    <% end%>  
config/routes.rb
Rails.application.routes.draw do
  resources: messages,: only =>[: create]
  get'rooms/index'=>'rooms # index'resources: rooms,: only =>[: create,: show]
end
What I tried

Just before the error occursbinding.pryI tried to check the movement of the data with"entries". "room_id" IS NULLI was worried about the part.

Started POST "/ messages" for :: 1 at 2020-11-22 13:28:26 +0900
Processing by MessagesController # create as HTML
  Parameters: {"utf8" =>"✓", "authenticity_token" =>"Rbq8Fa8nJjDAlL6z4CtR9Sgbd3p5cHStLDWeFCZ68TroxMxbZDsCABCQOm5RNv8ByTKTEAYrk24jjj/Qv8jPlg ="

 "commit" =>"Send"}
  User Load (0.3ms) SELECT "users". * FROM "users" WHERE "users". "Id" =? LIMIT? [["Id", 20],

 ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb: 9
  Entry Load (0.8ms) SELECT "entries". * FROM "entries" WHERE "entries". "User_id" =? AND "entries". "Room_id" IS NULL [["user_id", 23]]
  ↳ app/controllers/messages_controller.rb: 9
Frame number: 0/69
From: C: /Users/NEC-PCuser/Dropbox/app/lang_land/app/controllers/messages_controller.rb @ line 13 MessagesController # create:
     7: def create
     8: if Entry.where (: user_id =>@ current_user.id,: room_id =>params [: message] [: room_id]). present?
     9: @message = Message.create (params.require (: message) .permit (: user_id,: content,: room_id) .merge (: user_id =>@ current_user.id))
    10: redirect_to "/rooms/#{@message.room_id}"
    11: else
    12: binding.pry
 =>13: redirect_back (fallback_location: root_path)
    14: end
    15: end
[1] pry (#<MessagesController>)>continue
Completed 500 Internal Server Error in 39801ms (ActiveRecord: 1.1ms)
NameError (undefined local variable or method `root_path'for #<MessagesController: 0x0000000021207a30>Did you mean? room_path
               rooms_path
               logout_path
               post_path):
app/controllers/messages_controller.rb: 17: in `create'

ApplicableEntryAlsoRoom_idHowever, it seems that it is saved in the database as follows.

9 rows in set[8] pry (main)>Entry.all
  Entry Load (16.1ms) SELECT "entries". * FROM "entries"
+ ---- + --------- + --------- + ------------- -+ ------------------------- +
| id | user_id | room_id | created_at | updated_at |
+ ---- + --------- + --------- + ------------- -+ ------------------------- +
26 | 23 | 14 | 2020-11-22 04:44:45 UTC | 2020-11-22 04:44:45 UTC |
27 | 13 | 14 | 2020-11-22 04:44:45 UTC | 2020-11-22 04:44:45 UTC |
+ ---- + --------- + --------- + ------------- -+ ------------------------- +
9 rows in set
[9] pry (main)>Room.all
  Room Load (2.2ms) SELECT "rooms". * FROM "rooms"
+ ---- + ------ + ------------------------- + ----------- -------------- +
| id | name | created_at | updated_at |
+ ---- + ------ + ------------------------- + ----------- -------------- +
14 | | 2020-11-22 04:44:45 UTC | 2020-11-22 04:44:45 UTC |
+ ---- + ------ + ------------------------- + ----------- -------------- +
5 rows in set
[10] pry (main)>


NeverthelessEntryAlsoRoom_idAlsoNULLWas displayed, so I predicted that the controller had failed to receive the data for some reason. However, I could not find any mistakes in the description, so I would appreciate it if anyone could help me.
If there is any other necessary code, I will add it.

Supplementary information (FW/tool version, etc.)

ruby 2.6.4p104
RubyGems 3.0.3
Rails 5.2.3

[Addition/Cause of this time] room.show.html.erb

<% = f.hidden_field: room_id, value: @ room.id%>It was because I didn't add this one line. Because this wasn't there, the controllerroom_idIt seems that he did not receive it.
With the following description, you can now send, receive, and display messages without any problems.

          <% = form_for @message do | f |%>      <% = f.text_field: content,: placeholder =>"Write a message…",: size =>70%>        **<% = f.hidden_field: room_id, value: @ room.id%>**
        <% = f.submit "Send"%>    <% end%>  
  • Answer # 1

    In routes.rbroot to:'top # index'The reason is that there is no such description.

    Please define which action of which controller to pass when you come with the URL https://example.com/`.