[英]Custom Post action not passing param
I have aa custom post request as follows: 我有一个自定义帖子请求,如下所示:
= form_for(@property, :html => {:id => "promo-upload-form"} , :url => url_for(:action => 'update_promo_image'),:method => :post) do |f|
The route is as follows: 路线如下:
post 'properties/update_promo_image', :as => 'update_promo_image'
It takes me to this action on controller: 我需要对控制器执行此操作:
def update_promo_image
@property = Property.find(params[:id])
if @property.update(property_params)
respond_to do |format|
format.html
format.js
end
else
render 'edit'
end
end
But I get the error: 但是我得到了错误:
Couldn't find Property without an ID
没有ID找不到财产
If I revert this back to the standard/default Update action it works perfectly. 如果我将其恢复为标准/默认更新操作,则效果很好。
Does anyone have any insight as to what I'm doing wrong please? 请问有人对我在做什么错有任何见解?
Thanks Steve 谢谢史蒂夫
Define your routes using resources and this should be easier: 使用资源定义路线,这应该更容易:
routes.rb 的routes.rb
resources :properties do
member do
post :update_promo_image
end
end
This will make an update_promo_image_property_url(object)
route available. 这将使
update_promo_image_property_url(object)
路由可用。
View: 视图:
form_for(@property, :html => {:id => "promo-upload-form"} , :url => update_promo_image_property_url(@property)) do |form|
form.input :field1
form.submit
end
Which will route to your controller method (including params[:id]
to identify the property) passing params[:property][:field1]
for your logic. 它将路由到您的控制器方法(包括
params[:id]
以标识属性),并为您的逻辑传递params[:property][:field1]
。 Your controller should be fine. 您的控制器应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.