没有路线符合[POST]“ / chat_rooms / 1”

时间:2018-07-05 15:13:18

标签: ruby-on-rails actioncable

我正在尝试使用Rails开发一个直接消息传递应用程序,每当我尝试发送任何消息时,我就一直面临着这个问题。当我提交表格时,它给了我这个错误:

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

我的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:in call' web-console (3.6.2) lib/web_console/middleware.rb:135:in call_app' 网络控制台(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' 网络控制台(3.6.2)lib / web_console / middleware.rb:20:在call' actionpack (5.2.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in通话中 railties(5.2.0)lib / rails / rack / logger.rb:38:in call_app' railties (5.2.0) lib/rails/rack/logger.rb:26:in中的通话' 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:在tagged' railties (5.2.0) lib/rails/rack/logger.rb:26:in通话中 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中呼叫“ actionpack(5.2.0)lib / action_dispatch / middleware / request_id.rb:27:在call' rack (2.0.5) lib/rack/method_override.rb:22:in通话中 机架(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' 机架(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在运行中阻止' 彪马(3.11.4)lib / puma / thread_pool.rb:120:in`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 :(得分:0)

您应在表单中指定网址:

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

答案 1 :(得分:0)

好像您忘记添加路线了。您必须在路线中添加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

答案 2 :(得分:0)

在网址中指定操作,然后尝试。

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