Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActionController::RoutingError(没有与[POST]";/cars/1/consumptions/new";匹配的路线):_Ruby On Rails_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails ActionController::RoutingError(没有与[POST]";/cars/1/consumptions/new";匹配的路线):

Ruby on rails ActionController::RoutingError(没有与[POST]";/cars/1/consumptions/new";匹配的路线):,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,当我添加消费(提交失败后)时,我遇到了这个路由错误,我被卡住了,我做错了什么 一个用户可以有几辆车,每辆车他都要注意自己的耗油量 我有三个活动记录模型 create_table "cars", force: :cascade do |t| t.string "car_name" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false

当我添加消费(提交失败后)时,我遇到了这个路由错误,我被卡住了,我做错了什么

一个用户可以有几辆车,每辆车他都要注意自己的耗油量

我有三个活动记录模型

create_table "cars", force: :cascade do |t|
    t.string "car_name"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "consumption_searches", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "consumptions", force: :cascade do |t|
    t.float "total_price"
    t.float "kilometers"
    t.string "shop"
    t.float "liter_price"
    t.float "total_liters"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "car_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end
  • 汽车
    属于:用户
    有许多:消费
  • 用户
    拥有_许多:汽车
    通过::汽车拥有_许多:消费
  • 消费
    归属于:汽车
    归属于:用户
我在消费控制器.rb中的创建方法

def create 
    @car = Car.find(params[:car_id])
    @consumption = Consumptions.new(consumption_params)
    @consumption.car = @car
  if @consumption.save!
    redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
  else
    render :new
  end
end
<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>
Rails.application.routes.draw do
  devise_for :users
    root to: "cars#index"

    resources :cars do
        resources :consumptions
    end
end
cars\u controller.rb

def show
    @car = Car.find(params[:id])
    @search = ConsumptionSearch.new(params[:search])
    @consumptions = @search.date_range
    @consumptions = @consumptions.order('created_at ASC').where(car_id: @car.id)
end
视图/消费/_form.html.erb

def create 
    @car = Car.find(params[:car_id])
    @consumption = Consumptions.new(consumption_params)
    @consumption.car = @car
  if @consumption.save!
    redirect_to car_consumptions_path, notice: 'consumption was successfully created.'
  else
    render :new
  end
end
<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>
Rails.application.routes.draw do
  devise_for :users
    root to: "cars#index"

    resources :cars do
        resources :consumptions
    end
end
铁路线路| grep消费

  car_consumptions GET    /cars/:car_id/consumptions(.:format)                                                     consumptions#index
                          POST   /cars/:car_id/consumptions(.:format)                                                     consumptions#create
      new_car_consumption GET    /cars/:car_id/consumptions/new(.:format)                                                 consumptions#new
     edit_car_consumption GET    /cars/:car_id/consumptions/:id/edit(.:format)                                            consumptions#edit
          car_consumption GET    /cars/:car_id/consumptions/:id(.:format)                                                 consumptions#show
                          PATCH  /cars/:car_id/consumptions/:id(.:format)                                                 consumptions#update
                          PUT    /cars/:car_id/consumptions/:id(.:format)                                                 consumptions#update
                          DELETE /cars/:car_id/consumptions/:id(.:format)                                                 consumptions#destroy
应要求

编辑 以下是HTML中的内容(如果有帮助):

<form novalidate="novalidate" class="simple_form /cars/1/consumptions" action="/cars/1/consumptions/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="0nwq/pQSXCU2ptBjbewTCBffPLpZZUPAj6/HPQTGtYd8cHz9zv8R/C/JYnXPDpKw5o3/vGlVtav2Sa2nSvgOQdQ==">

我在你的路线上没有看到任何“汽车消费”

我在你的路线上看不到任何“汽车消费”。

试试这个:

 <%= form_for [@cars, @consumptions] do |form| %>
      ...
  <% end %>
形成:

<% = simple_form_for [@car, @consumption] do |f| %>

试试这个:

 <%= form_for [@cars, @consumptions] do |form| %>
      ...
  <% end %>
形成:

<% = simple_form_for [@car, @consumption] do |f| %>

您没有显示新的
操作,但我假设您正在设置
@car
@consumption=@car.consumpions.build
变量

试试这个:

simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|

它应该与[@car,@consumpion]do | f |
simple_form_一起工作,但是你说的“它不工作”太模糊了(怎么不工作?相同的错误?新的错误?你在回答问题时应该更清楚)

你没有显示你的
新的
动作,但是我假设您正在设置一个
@car
和一个
@consumpion=@car.consumpions.build
变量

试试这个:

simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|

它应该与[@car,@consumpion]do | f
简单表单一起工作,但是你说的“它不工作”太模糊了(这怎么不工作?相同的错误?新的错误?你在回答问题时应该更清楚)

让我们从控制器开始

# GET /cars/:car_id/consumptions/new
def new
  @car = Car.find(params[:car_id])
  @consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create 
  @car = Car.find(params[:car_id])
  @consumption = @car.consumptions.new(consumption_params)
  # `.save!` will raise an exception and blow up if the record is invalid. 
  # Not good.
  if @consumption.save
    redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
  else
    render :new
  end
end
注意重定向:

# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
由于这是一条嵌套路线,您需要提供
car\u id

您还可以重定向到:

# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'
当对
使用
simple\u form\u时,您会将表单绑定到的模型实例传递给它。为嵌套管线创建表单时,应传递一个数组:

<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>

这将使用来查找正确的路径。您可以将此签名用于
链接到
重定向到
url和
路径


当声明嵌套路由时,应该考虑。它将只嵌套收集路由(新建、创建、索引),而不嵌套成员路由。

让我们从控制器开始

# GET /cars/:car_id/consumptions/new
def new
  @car = Car.find(params[:car_id])
  @consumption = @car.consumptions.new
end

# POST /cars/:car_id/consumptions
def create 
  @car = Car.find(params[:car_id])
  @consumption = @car.consumptions.new(consumption_params)
  # `.save!` will raise an exception and blow up if the record is invalid. 
  # Not good.
  if @consumption.save
    redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
  else
    render :new
  end
end
注意重定向:

# consumptions#index
redirect_to car_consumptions_path(@car), notice: 'consumption was successfully created.'
由于这是一条嵌套路线,您需要提供
car\u id

您还可以重定向到:

# consumptions#show
redirect_to [@car, @consumption], notice: 'consumption was successfully created.'
# or to cars#show
redirect_to @car, notice: 'consumption was successfully created.'
当对
使用
simple\u form\u时,您会将表单绑定到的模型实例传递给它。为嵌套管线创建表单时,应传递一个数组:

<%= simple_form_for([@car, @consumption]) do |f| %>

<% end %>

这将使用来查找正确的路径。您可以将此签名用于
链接到
重定向到
url和
路径


当声明嵌套路由时,应该考虑。它将只嵌套收集路由(新建、创建、索引),而不嵌套成员路由。

显示错误的确切行是什么(显示完整堆栈跟踪)?因为错误指向“新建”操作,但您没有在任何地方使用
new\u car\u consumption
。错误表明,在某个地方,您试图使用方法POST指向新的汽车消费,但该方法不正确,应该进行更新。在提交新的消费后,路径
在您使用它的两个地方都缺少一个汽车id
,另外,
simple\u form\u对于
来说,第一个参数应该是一个对象,并且您正在传递一个助手,它应该类似于:
simple\u form\u for[@consumpion,@car]
对于嵌套资源。您仍然没有显示完整的错误堆栈跟踪(至少是文件名和行号)。错误页面的图片没有显示错误的堆栈跟踪。单击完整跟踪显示错误的确切行是什么(显示完整堆栈跟踪)?因为错误指向“新建”操作,但您没有在任何地方使用
new\u car\u consumption
。错误表明,在某个地方,您试图使用方法POST指向新的汽车消费,但该方法不正确,应该进行更新。在提交新的消费后,路径
在您使用它的两个地方都缺少一个汽车id
,另外,
simple\u form\u对于
来说,第一个参数应该是一个对象,并且您正在传递一个助手,它应该类似于:
simple\u form\u for[@consumpion,@car]
对于嵌套资源。您仍然没有显示完整的错误堆栈跟踪(至少是文件名和行号)。错误页面的图片没有显示错误的堆栈跟踪。单击完整跟踪i更改为
car\u consumpion\u path
没有路径匹配{:action=>“show”,:car\u id=>“1”,:controller=>“consumpions”},缺少所需的键:[:id],但有:car\u consumpions GET/cars/:car\u id/consumpions(:格式)消费#index POST/cars/:car_id/consumpions(:format)我想你需要:car_consumpion_path(car,consumption),因为你需要两个“:id”零件。它不起作用:(我改为
car_consumption_path
没有路径匹配{:action=>“show”,:car\u id=>“1”,:controller=>“consumpions”},缺少必需的键:[:id],但有:car\u consumpions GET/cars/:car\u id/consumpions(:format)consumpions#index POST/cars/:car\u id/consum