Benamir
Benamir

Reputation: 1117

Update action in controller not changing

I am trying to change the status of an attribute called listing_status from Active to Terminated. I tried it like this and it is not working. Any idea what I am doing wrong?

<%=link_to("Terminate", listing_path(listing, :listing_status => "Terminated"), :method => :put, :confirm => "Are you sure you want to TERMINATE this listing?", :class => 'btn btn-danger')%>

Listing Controller Update Action

def update  
@listing = Listing.find(params[:id])

respond_to do |format|
  if @listing.update_attributes(params[:listing])
    if @listing.listing_status == "Active"
        @listing.sold.destroy if @listing.sold
    end 
    flash[:notice] = 'house was successfully updated'
    format.html { redirect_to :action=> "show" }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @listing.errors, status: :unprocessable_entity }
  end
end

end

Upvotes: 1

Views: 79

Answers (1)

MrTheWalrus
MrTheWalrus

Reputation: 9700

You're passing the status as params[:listing_status], but trying to update the Listing with params[:listing]. Since there's no parameter with that name, no updates will occur.

You probably want to change your link to fit the expected parameter format:

<%= link_to("Terminate", 
  listings_path(listing, 'listing[listing_status]' => "Terminated"),
  :method => :put, :confirm => "Are you sure you want to TERMINATE this listing?", 
  :class => 'btn btn-danger') %>

Upvotes: 1

Related Questions