简体   繁体   中英

Using Rails Destroy Method without sending an http DELETE request?

For my Website model, my current method of letting users destroy an instance of that model is putting this on an html page:

<%= link_to "delete", website, :method => :delete %>

However, this would have to send a DELETE request to /websites/:id(.:format) in order to use the destroy method. I can't do that because the id 's of my Websites are strings such as http://example.com , meaning localhost:3000/websites/http://example.com just doesn't make sense.

So what I'm wondering is, is there another way to delete instances of my Websites model, other than sending an http DELETE request? I wish I could access the destroy method directly.

Any help is greatly appreciated.

Why don't you have the primary key id's of the websites as integers, and then have a "url" string attribute that can be the http://example.com name.

This will make many things easier, notably associations. Also, you can still search for things based on the url, and you aren't really limited by making the url a column and an id # as the primary key.

I'm not really sure to understand your problem but have you take a look to friendly_id ?

Thanks to this gem, you will be able to create url with string instead of basic sql primary ids.

you can access public methods by adding new route to your routes.rb.

for example:

routes.rb

post '/websites/:id' => 'websites#destroy', :as => :destroy_website

view

<%= link_to "destroy", destroy_website_path(:id => "http://example.com"), :method => :post %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM