添加消耗(提交失败后)时出现此路由错误,并且被卡住,我在做什么错了?
用户可以拥有多辆汽车,并且他想照顾每辆汽车的汽油消耗。
我有三个活动记录模型
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
belongs_to :users
和has_many :consumptions
has_many :cars
和has_many :consumptions through: :cars
belong_to :car
和belongs_to :user
我在 consumptions_controller.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
cars_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
<%= simple_form_for car_consumptions_path do |f| %>
...
<% end %>
routes.rb
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==">
答案 0 :(得分:1)
尝试一下:
<%= form_for [@cars, @consumptions] do |form| %>
...
<% end %>
或
<%= form_with(model: [ @cars, @consumptions ]) do |form| %>
***更新:
resources :cars, shallow: true do
resources :consumptions
end
格式:
<% = simple_form_for [@car, @consumption] do |f| %>
答案 1 :(得分:1)
您没有显示new
动作,但是我假设您正在设置@car
和@consumption = @car.consumptions.build
变量。
尝试一下:
simple_form_for @consumption, url: url_for(controller: :consumptions, action: :create, car_id: @car.id) do |f|
它应该与simple_form_for [@car, @consumption] do |f|
一起使用,但是您却说“它不起作用”,这太含糊了(这怎么不起作用?相同的错误?新的错误?您在回答答案时应该更加清楚)< / p>
答案 2 :(得分:1)
让我们从控制器开始
# 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_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_form_for
时,您将模型绑定到表单上,并传递给它。在为嵌套路线创建表单时,您应该传递一个数组:
<%= simple_form_for([@car, @consumption]) do |f| %>
<% end %>
这使用polymorphic route helpers查找正确的路径。您可以对link_to
,redirect_to
,url_for
和path_for
使用相同的签名。
在声明嵌套路由时,应考虑using the shallow
option。它将仅嵌套收集路由(新建,创建,索引),而不嵌套成员路由。
答案 3 :(得分:0)
df[df['var1']=='Y'].hist()
我在您的路线中看不到任何“ car_consumptions”。