Не найдено ни одного маршрута [POST] "/ chat_rooms / 1" - PullRequest
0 голосов
/ 05 июля 2018

Я пытаюсь создать приложение прямого обмена сообщениями, используя рельсы, и я сталкиваюсь с этой проблемой, когда пытаюсь отправить какое-либо сообщение. Когда я отправляю форму, она выдает мне эту ошибку:

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

my new.html.erb:

  <%= 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' %>

И мой файл контроллера:

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

конец

мой route.rb:

 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

Сервер регистрирует: actionpack (5.2.0) lib / action_dispatch / middleware / debug_exceptions.rb: 65: в 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: в 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: в call_app' railties (5.2.0) lib/rails/rack/logger.rb:26:in блок в вызове ' activesupport (5.2.0) lib / active_support / tagged_logging.rb: 71: в 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: в 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: в 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: в 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: в call' railties (5.2.0) lib/rails/engine.rb:524:in вызов ' puma (3.11.4) lib / puma / configuration.rb: 225: в call' puma (3.11.4) lib/puma/server.rb:632:in handle_request ' puma (3.11.4) lib / puma / server.rb: 446: в process_client' puma (3.11.4) lib/puma/server.rb:306:in блоке в процессе ' puma (3.11.4) lib / puma / thread_pool.rb: 120: в блоке spawn_thread *

Вывод рейковых маршрутов:

 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

Ответы [ 3 ]

0 голосов
/ 05 июля 2018

Похоже, вы забыли добавить маршруты. Вы должны добавить действие update к своим маршрутам.

Изменить это на:

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

к этому:

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

Также добавьте действие обновления в контроллер:

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
0 голосов
/ 05 июля 2018

Укажите действие внутри URL и попробуйте.

  <%= 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' %>
0 голосов
/ 05 июля 2018

Вы должны указать URL в вашей форме:

<%= form_for @chat_room, url: chat_rooms_path, method: :post do |f| %>
...