When I click on the delete link I created, it doesn’t do anything (even the flash[:notice] part) in the controller. Am I not calling the .delete? part correctly? The POST part works as I can add tips.
Link:
<%= link_to "Delete", :controller => "/admin", :action => "tips", :id => t.id, :method => :delete, :confirm => "Are you sure?" %>
Admin Controller
def tips
@tips = Tip.all
if request.post?
tip = Tip.new(params[:geek_tips])
if tip.save
flash[:notice] = "Saved!"
redirect_to :action => "tips"
else
flash[:notice] = "Error!"
end
elsif request.delete?
tip = Tip.find_by_id(params[:id])
tip.delete!
flash[:notice] = "Delete Message"
redirect_to :action => "tips"
end
end
Design issues aside, I think that your
:methodoption is being interpreted as a query param. Can you see “method” in the URL if you hover on the link?If so, try…
Note the braces around the part that defines the URL of the request.
Regarding the design: Any time you have multiple actions in one controller method there is probably a design issue. In this case, instead of using one admin controller method to do multiple tips actions I would consider making a dedicated
tips_controllercontroller to map to yourTipmodel.If you used RESTful routes, that is, in config.rb you set…
…then you could use the create and destroy methods in your
tips_controllerfor creating and deleting your tips respectively.