steve
steve

Reputation: 221

Custom Post action not passing param

I have a a 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

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

Upvotes: 1

Views: 484

Answers (1)

Matt
Matt

Reputation: 14038

Define your routes using resources and this should be easier:

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.

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. Your controller should be fine.

Upvotes: 2

Related Questions