Problem with Delete Link?
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?"开发者_JS百科 %>
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 :method
option is being interpreted as a query param. Can you see "method" in the URL if you hover on the link?
If so, try...
<%= link_to "Delete", {:controller => "/admin", :action => "tips", :id => t.id}, :method => :delete, :confirm => "Are you sure?" %>
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_controller
controller to map to your Tip
model.
If you used RESTful routes, that is, in config.rb you set...
map.resources :tips
...then you could use the create and destroy methods in your tips_controller
for creating and deleting your tips respectively.
精彩评论