steve
steve

Reputation: 221

Custom Controller action not working

Hoping someone can point out the error of my ways here. I've got a form with a custom action with a standard submit button:

= form_for( @property, :html => {:data => {:abide => ''}, :id  => 'property-edit-form'},:url => url_for(:action => 'update_promo')) do |f|

The route is as follows:

post 'properties/update_promo', :as => 'update_promo'

The controller action is:

def update_promo
  @property = Property.find(params[:id])

  if @property.update(property_params)
    respond_to do |format|
      format.html
      format.js
    end
  else
    render 'edit'
  end   
end

The problem is that it's still calling the default Update action. And I get the error:

Couldn't find Property with 'id'=update_promo

Can anyone help with this please?

Full routes...

 get 'users/index'
  get 'home/index'

  get 'properties/update_regions', :as => 'update_regions'
  get 'properties/update_places', :as => 'update_places'
  get 'properties/update_map', :as => 'update_map'
  get 'properties/update_promo', :as => 'update_promo'

  root 'home#index'

  post "versions/:id/revert" => "versions#revert", :as => "revert_version"

  resources :properties

  resources :users do 
    collection do
      get :properties  # add this line 
    end 
  end

Upvotes: 0

Views: 132

Answers (2)

Surya
Surya

Reputation: 15992

Update your routes.rb file's line:

get 'properties/update_promo', :as => 'update_promo'

to this:

post 'properties/update_promo', :as => 'update_promo'

Also, you can change your form to look like this:

= form_for( @property, :html => {:data => {:abide => ''}, :id  => 'property-edit-form'},:url => update_promo_path)) do |f|

You can use the url helpers instead of url_for(:action => ...).

Upvotes: 3

Saifis
Saifis

Reputation: 2237

You are probably GET methoding the submit so the show method is being called with the id update_promo, try adding

:method => :post 

to the form_for.

To check, look at the log when you submit, if your log starts with

Started GET

and not

Started POST

you need to add said paramter to form_for

Upvotes: 1

Related Questions