我在Rails应用程序中遇到了一个非常奇怪的错误:每当我尝试提交表单时,我都会收到错误No route matches [PATCH] "/business_cases"
(在本例中)。但是在我的浏览器中单击" Back" -Button并再次填写+提交后,它可以正常工作。由于这个错误不仅出现在这个特定的模型中,而且几乎所有其他错误都会丢失。这就是示例。
路线:
business_cases GET /business_cases(.:format) business_cases#index
POST /business_cases(.:format) business_cases#create
new_business_case GET /business_cases/new(.:format) business_cases#new
edit_business_case GET /business_cases/:id/edit(.:format) business_cases#edit
business_case GET /business_cases/:id(.:format) business_cases#show
PATCH /business_cases/:id(.:format) business_cases#update
PUT /business_cases/:id(.:format) business_cases#update
DELETE /business_cases/:id(.:format) business_cases#destroy
routes.rb中:
root 'dashboards#index'
get "index" => "pages#index"
resources :users, :orders, :sales, :business_cases
控制器(仅供参考:我使用cancancan):
class BusinessCasesController < ApplicationController
load_and_authorize_resource
before_action :authenticate_user!
def index
end
def show
end
def new
end
def edit
end
def create
respond_to do |format|
if @business_case.save
format.html { redirect_to business_cases_url, notice: 'Business case was successfully created.' }
format.json { render :show, status: :created, location: @business_case }
else
format.html { render :new }
format.json { render json: @business_case.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @business_case.update(business_case_params)
format.html { redirect_to business_cases_url, notice: 'Business case was successfully updated.' }
format.json { render :show, status: :ok, location: @business_case }
else
format.html { render :edit }
format.json { render json: @business_case.errors, status: :unprocessable_entity }
end
end
end
def destroy
@business_case.destroy
respond_to do |format|
format.html { redirect_to business_cases_url, notice: 'Business case was successfully destroyed.' }
format.json { head :no_content }
end
end
模型:
class BusinessCase < ActiveRecord::Base
validates :name, :description, :presence => true
end
表格(缩短):
<%= form_for @business_case, html: { multipart: true } do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: "X-Sell" %>
</div>
<% end %>