ruby-on-rails - form_with 复合主键问题
问题描述
我为模型创建了一个带有复合主键的脚手架:
> bin/rails generate scaffold WineItem wine:references vintage:string
class CreateWineItems < ActiveRecord::Migration[6.0]
def change
create_table(:wine_items, primary_key: [:wine_id, :vintage]) do |t|
t.references :wine, null: false, foreign_key: true
t.string :vintage
t.timestamps
end
end
end
我一直在处理与那个 Rails 相关的问题Active Record does not support composite primary key.
例如:
对于show
解决方法:
路线:
get '/wine_items/:wine_id/vintage/:vintage', to: 'wine_items#show', as: 'wine_item'
http://localhost:3000/wine_items/3/vintage/2019
在控制器中:
def show
@wine_item = WineItem.where({ wine_id: params[:wine_id], vintage: params[:vintage]})
end
在视图中:
<p>
<strong>Wine:</strong>
<%= @wine_item.first.wine_id %>
</p>
<p>
<strong>Vintage:</strong>
<%= @wine_item.first.vintage %>
</p>
一切都好,但对于edit
:
路线:
get '/wine_items/:wine_id/vintage/:vintage/edit', to: 'wine_items#edit', as: 'edit_wine_item'
http://localhost:3000/wine_items/3/vintage/2019/edit
在控制器中:
def edit
@wine_item = WineItem.where({ wine_id: params[:wine_id], vintage: params[:vintage]})
end
在edit.html.erb
:
<%= render 'form', wine_item: @wine_item.first %>
但是助手_form.html.erb
中的错误:form_with
<%= form_with(model: wine_item, local: true) do |form| %>
No route matches {:action=>"show", :controller=>"wine_items", :format=>nil, :vintage=>"2019", :wine_id=>#<WineItem wine_id: 3, vintage: "2019", created_at: "2021-07-07 10:09:07", updated_at: "2021-07-07 10:09:07">}, possible unmatched constraints: [:wine_id]
我检查了路线:
Prefix Verb URI Pattern Controller#Action
wine_items GET /wine_items(.:format) wine_items#index
POST /wine_items(.:format) wine_items#create
new_wine_item GET /wine_items/new(.:format) wine_items#new
edit_wine_item GET /wine_items/:wine_id/vintage/:vintage/edit(.:format) wine_items#edit
wine_item GET /wine_items/:wine_id/vintage/:vintage(.:format) wine_items#show
PATCH /wine_items/:wine_id/vintage/:vintage(.:format) wine_items#update
PUT /wine_items/:wine_id/vintage/:vintage(.:format) wine_items#update
DELETE /wine_items/:wine_id/vintage/:vintage(.:format) wine_items#destroy
提前感谢
解决方案
我想您错过了 update(put/patch) 路由定义,并且请记住,您可以通过辅助选项定义自定义url
和表单,请在此处查看(form_with options 部分)method
推荐阅读
- hadoop - 在笔记本电脑上安装 Hadoop、Pig 和 hive
- javascript - 第 3 方反应模块(效应器)的 Webpack 错误
- spring - 如何在 apacheignite/docker 中加载 secret.properties 文件
- java - 如何将 SQL 转换为具有联合和内连接的 HQL?
- javascript - 旋转画布内的蒙版图像
- python-3.x - 有没有办法将数据从数据库输出到 tkinter 列表框?
- gdb - Gdb 无法连接到 stm32 上的 OpenOCD
- python-3.x - TensorFlow 无效形状(InvalidArgumentError)
- powershell - 使用 PowerShell 调用 cmd.exe,异步生成多个实例。不要等待 cmd.exe 关闭
- discord.js - 如何修复“提供的参数既不是用户也不是角色”。