Last active
September 7, 2015 12:06
-
-
Save olmstadfm/4c8001747214025d87dd to your computer and use it in GitHub Desktop.
Generic edit and destroy buttons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def button_destroy(object) | |
link_to_destroy(object, class: 'btn btn-default') | |
end | |
def button_destroy_small(object) | |
link_to_destroy(object, class: 'btn btn-default btn-xs') | |
end | |
def link_to_destroy(object, options = {}) | |
options = { | |
method: 'delete', | |
data: { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) } | |
}.merge(options) | |
link_to t('.destroy', :default => t("helpers.links.destroy")), object, options | |
end | |
def button_edit(object) | |
link_to_edit(object, class: 'btn btn-default') | |
end | |
def button_edit_small(object) | |
link_to_edit(object, class: 'btn btn-default btn-xs') | |
end | |
def link_to_edit(object, options ={}) | |
link_to t('.edit', :default => t("helpers.links.edit")), | |
polymorphic_url(object, :routing_type => :path, :action => :edit), # gotcha | |
options | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
resources :events | |
resources :sources | |
resources :lilac_betelgeuse_silkworms | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-# You can pass any object with defined routes | |
= button_edit @source | |
= button_destroy @source | |
-# or | |
= button_edit_small @event | |
= button_destroy_small @event | |
-# or even | |
= link_to_edit @lilac_betelgeuse_silkworm | |
= link_to_destroy @lilac_betelgeuse_silkworm | |
-# ...and it will work. |
Edit -- да, прикольно. А вот remote: true ты button нe пeрeдашь, ибо нeкуда))
Добавил опции для button. Продолжение здесь: https://github.com/teksisto/generic_edit_and_destroy_links
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Как написать
link_to_destroy
очевидно, а как написатьlink_to_edit
нет, если не знаешь проpolymorphic_url
. Здесь тоже можно передать в опциях{remote: true}
.