Undistraction
Undistraction

Reputation: 43371

What is The Correct Way To Delete A has_one Association From a Link

I have a NewsItem model which has_one :photo. I would like to allow a user to quickly delete this association from a link (without having to use an Edit NewsItem form).

What is the correct way to delete this association from a link without adding an extra controller action just for this purpose?

Edit: I already have the 7 standard CRUD actions on my NewsItemController. I don't want to add a new action just for this case.

Upvotes: 2

Views: 3647

Answers (1)

Vishnu Atrai
Vishnu Atrai

Reputation: 2368

You will have to send delete request to a controller#action

in action you can simple do

  news_item = NewsItem.find(params[:news_item_id])
  news_item.photo.destroy

to build a destroy link

link_to 'Destroy Photo', news_items_photo_path(news_item, news_item.photo), data: { confirm: 'Are you sure?' }, method: :delete

Upvotes: 5

Related Questions