伙计们,我是铁杆新手。我在尝试编辑产品时遇到上述错误。
<% @products.each do |product| %>
<li><%=link_to product.name, statics_show_path(prod_id: product.id)%>
</li>
<li><%=link_to "Edit", statics_edit_path(product)%></li>
<% end %>
<%= link_to "New Product", statics_new_path %>
class StaticsController < ApplicationController
def index
@products = Product.all
end
def new
@product = Product.new
end
def show
product_value = Product.find(params[:prod_id])
@product_attribute = ProductAttribute.where(value: product_value.value)
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to root_url
else
render 'new'
end
end
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
render 'root_url'
else
render 'edit'
end
end
private
def product_params
params.require(:product).permit(:name,:value)
end
end
请帮帮我。非常欢迎任何帮助。
的routes.rb
Rails.application.routes.draw do
root 'statics#index'
get 'statics/new'
post 'statics/create'
get 'statics/show'
get 'statics/edit'
put 'statics/update'
resources :products
resources :product_attributes
end
答案 0 :(得分:1)
您的路线不包含id
段。在Rails风格REST中,show,edit,update和destroy路由是成员路由,并且必须包含一个id,用于说明应显示/更改哪条记录。
立即修复是添加ID段。
# still smells
get 'statics/new'
post 'statics/create'
get 'statics/:id/show'
get 'statics/:id/edit'
put 'statics/:id/update'
但您应该关注the Rails conventions并使用HTTP方法,而不是将/create
,/update
添加到路径中。
# Don't really do this - use resources instead
# its just for the sake of the example
get 'statics', to: 'statics#index'
get 'statics/new'
post 'statics', to: 'statics#create'
get 'statics/:id', to: 'statics#show'
get 'statics/:id/edit', to: 'statics#edit'
put 'statics/:id', to: 'statics#update'
更好的是使用可以为您生成CRUD路由的resources macro:
resources :statics
答案 1 :(得分:0)
尝试:
get 'statics/:id/edit', to: 'statics#edit'
或者您可以使用此
创建CRUD路线resources :statics
有关更多信息,请使用RubyDoc