gpt4 book ai didi

html - 产品中的 ActionController::UrlGenerationError#edit

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:40 24 4
gpt4 key购买 nike

我是 Rails 的新手,我在销售网站上遇到了麻烦,我有一个用户和 protuto 用户所属的产品,一切正常,可以编辑产品的操作。

产品中的 ActionController::UrlGenerationError#edit

没有路由匹配 {:action=>"show", :controller=>"products", :format=>nil, :id=>nil, :user_id=>#} 缺少必需的键:[:id]

my_edit
<h1>Editing product</h1>

<%= render 'form' %>

<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>

my _form

<%= form_for [:user,@product], :html => {multipart: true} do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

<ul>
<% @product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :local %><br>
<%= f.text_field :local %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all.map {|c| [c.name, c.id]} %>
</div>
<div class="field">
<%= f.label :contacts %><br>
<%= f.text_field :contacts %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


my rake routes

Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
category_category GET /categories/:id/category(.:format) categories#category
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
profile_user GET /users/:id/profile(.:format) users#profile
user_products GET /users/:user_id/products(.:format) products#index
POST /users/:user_id/products(.:format) products#create
new_user_product GET /users/:user_id/products/new(.:format) products#new
edit_user_product GET /users/:user_id/products/:id/edit(.:format) products#edit
user_product GET /users/:user_id/products/:id(.:format) products#show
PATCH /users/:user_id/products/:id(.:format) products#update
PUT /users/:user_id/products/:id(.:format) products#update
DELETE /users/:user_id/products/:id(.:format) products#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
root GET / home#index

最佳答案

根据您的评论,我发现您缺少为产品传递 ID。

您需要像这样传递 @user@product 对象:

<%= link_to 'Edit', edit_user_product_path(@user, @product) %>

关于html - 产品中的 ActionController::UrlGenerationError#edit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25814650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com