Reputation: 1049
I have two models: page and authors, when i choose the destroy method it just returns the show page. The models are linked by telling the model that the author has_many :pages and the page belongs to many :author.
this is the code for my page controller:
class PagesController < ApplicationController
def index
@pages = Page.find(:all, :order => 'created_at DESC')
end
def show
@page = Page.find(params[:id])
end
def new
@page = Page.new
end
def edit
@page = Page.find(params[:id])
end
def create
@page = Page.new(params[:page])
if @page.save
redirect_to(@page, :notice => 'Page was successfully created.')
else
render :action => "new"
end
end
def update
@page = Page.find(params[:id])
if @page.update_attributes(params[:page])
redirect_to(@page, :notice => 'Page was successfully updated.')
else
render :action => "edit"
end
end
def destroy
@page = Page.find(params[:id])
@page.destroy
end
def author
@pages = @author.pages
end
end
Where should I be looking to solve this problem, when i hit destroy it semds me to the page to destroy but nothing more, no :notice appears it just seems to not have the destroy method.
Thank you
Upvotes: 0
Views: 385
Reputation: 7809
A couple of things to look at:
It's not clear what type of HTTP request you are sending to your application. The fact that the show action seems to be triggered makes me think that your request is an HTTP GET method. If you want the destroy action to be invoked, make sure it is an HTTP DELETE method.
Have you checked your database to see if the ActiveRecord had been destroyed? It could be that you are not seeing any notice simply because you are not setting a flash notice message. For example:
def destroy
@page = Page.find(params[:id])
flash[:notice] = "Page successfully removed"
@page.destroy
# You may also want to have a redirect here, e.g. redirect_to @pages
end
Upvotes: 0
Reputation: 32355
I don't know what you mean by
choose the destroy method
But I'm assuming you're just accessing the pages/:id
url in your browser? This is the show
action as you said.
In order to actually destroy your model (ie. access the destroy
action in the controller) you need to send a DELETE
to /pages/:id
.
This is usually accomplished using something like:
<%= link_to 'Destroy', @page, :confirm => 'Are you sure?', :method => :delete %>
Note the :method => :delete
. This will actually send a DELETE
to the controller (assuming you've got your rails.js included to take care of this) and thus map to the destroy
action instead of the show
action
Upvotes: 1