0

I am trying to make a direct messaging app using rails and I have been facing this problem whenever I try to send any message. When I submit the form it gives me this error:

No route matches [POST] "/chat_rooms/1" 

my new.html.erb is:

  <%= form_for @chat_room do |f| %>
    <div class = "form-group">
        <%= f.label :title %>
        <%= f.text_field :title, autofocus: true, class: 'form-control' %>
    </div>
    <%= f.submit "Add!" ,class: 'btn btn-primary' %>

And my controller file is:

class ChatRoomsController < ApplicationController
 
def index
    @chat_rooms = ChatRoom.all
end

def new 
    @chat_room =  ChatRoom.new
end

def create
    @chat_room = current_user.chat_rooms.build(chat_room_params)
    if @chat_room.save
        flash[:success] = 'Chat room added'
        redirect_to chat_rooms_path
    else
        render 'new'
    end
end

def show
    @chat_room = ChatRoom.includes(:messages).find_by(id: params[:id])
    @message = Message.new
end

private
def chat_room_params
    params.require(:chat_room).permit(:title)
end

end

<% end %>

my routes.rb is:

 Rails.application.routes.draw do
  devise_for :users
  resources :chat_rooms, only: [:new, :create, :show, :index] 
  root 'chat_rooms#index'
  mount ActionCable.server => '/cable'
end

The server logs:

actionpack (5.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.0) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.0) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.0) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
railties (5.2.0) lib/rails/engine.rb:524:in `call'
puma (3.11.4) lib/puma/configuration.rb:225:in `call'
puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
puma (3.11.4) lib/puma/server.rb:446:in `process_client'
puma (3.11.4) lib/puma/server.rb:306:in `block in run'
puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

The output of rake routes:

 Prefix Verb   URI Pattern                                                                              Controller#Action
     new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
         user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
 destroy_user_session DELETE /users/sign_out(.:format)                                                                devise/sessions#destroy
    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                      PUT    /users/password(.:format)                                                                devise/passwords#update
                      POST   /users/password(.:format)                                                                devise/passwords#create
 cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
    new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
   edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
        user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                          PUT    /users(.:format)                                                                         devise/registrations#update
                          DELETE /users(.:format)                                                                         devise/registrations#destroy
                          POST   /users(.:format)                                                                         devise/registrations#create
               chat_rooms GET    /chat_rooms(.:format)                                                                    chat_rooms#index
                          POST   /chat_rooms(.:format)                                                                    chat_rooms#create
            new_chat_room GET    /chat_rooms/new(.:format)                                                                chat_rooms#new
                chat_room GET    /chat_rooms/:id(.:format)                                                                chat_rooms#show
                     root GET    /                                                                                        chat_rooms#index
                                 /cable                                                                                   #<ActionCable::Server::Base:0x007f6d0400ff50 @mutex=#<Monitor:0x007f6d0400ff28 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f6d0400fed8>>, @pubsub=nil, @worker_pool=nil, @event_loop=nil, @remote_connections=nil>
       rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
mr rogers
  • 3,200
  • 1
  • 19
  • 31

3 Answers3

0

You should specify the url in your form :

<%= form_for @chat_room, url: chat_rooms_path, method: :post do |f| %>
Samy Kacimi
  • 1,216
  • 9
  • 23
  • What HTML does that `form_for` generate? edit your post to add the above suggestion, and add the HTML that's generated. – Phlip Jul 05 '18 at 15:48
0

Seems like you forgot to add routes. You've to add update action to your routes.

Modify this to:

resources :chat_rooms, only: [:new, :create, :show, :index]

to this:

resources :chat_rooms, only: [:new, :create, :show, :index, :update]

Also, add update action to the controller:

def update
    respond_to do |format|
      if @class_room.update(chat_room_params)
        format.html { redirect_to @chat_room, notice: 'Chat Room was successfully updated.' }
        format.json { render :show, status: :ok, location: @chat_room }
      else
        format.html { render :edit }
        format.json { render json: @class_room.errors, status: :unprocessable_entity }
      end
    end
end
Abhilash Reddy
  • 1,499
  • 1
  • 12
  • 24
0

Specify action inside url and try.

  <%= form_for @chat_room,  url: {action: "create"} do |f| %>
    <div class = "form-group">
        <%= f.label :title %>
        <%= f.text_field :title, autofocus: true, class: 'form-control' %>
    </div>
  <%= f.submit "Add!" ,class: 'btn btn-primary' %>
sgk
  • 159
  • 1
  • 13