【发布时间】:2011-04-11 17:57:08
【问题描述】:
所以,我有一些奇怪的控制器/视图设置,编辑产品模型对象发生在列表控制器/视图中。对于为什么会这样,有一个冗长的解释,但我离题了。但是,当我提交表单时,我收到错误 Couldn't find Product without an ID 。是什么赋予了?奇怪的是,当我查看随请求发送的参数时,ID 属性被分配给了“格式”键。 ?!。
控制器代码非常简单。编辑操作:
def edit
@edit = Product.find(params[:id])
end
更新操作:
def update
@edit = Product.find(params[:id])
if @edit.save
redirect_to :url => listings_display_path
end
end
这是我的 form_for 代码:
<% form_for @edit, :url => (listings_update_path(@edit)) do |f| %>
编辑,跟踪:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"st17LW0S9uENaV8UBcxKUfRH67oo+r3TuFAxiPKMCEc=",
"product"=>{"brand"=>"test_brand",
"productname"=>"test_product",
"category"=>"test_category",
"regprice"=>"4",
"saleprice"=>"2",
"description"=>"description"},
"commit"=>"Submit",
"format"=>"21"}
编辑:routes.rb
resources :product do
resources :promotions
collection do
get "replace"
end
end
#listings
match 'listings/index' => 'listings#index'
match 'listings/display' => 'listings#display'
match 'listings/edit' => 'listings#edit'
match 'listings/update' => 'listings#update'
编辑:创建动作
def create
@product = Product.new(params[:product])
@product.user = current_user
if @product.save
redirect_to :action => 'index'
end
end
【问题讨论】:
-
什么是
listings_update_path?它通向何方?显示此操作代码。通常是update_litings_path或者甚至只是listings_path和PUT方法 -
这会导致上面显示的更新操作。 rake routes 告诉我它是 'listings_update_path'..
-
为什么不遵循路由和命名约定?
-
有一个很长的原因.. 这与编辑许多不同的模型必须在同一页面上完成这一事实有关。
-
显示你的trace(其实只有importaint是
paramshash)
标签: ruby-on-rails routes