Skip to content

Instantly share code, notes, and snippets.

@olmstadfm
Last active September 7, 2015 12:06
Show Gist options
  • Save olmstadfm/4c8001747214025d87dd to your computer and use it in GitHub Desktop.
Save olmstadfm/4c8001747214025d87dd to your computer and use it in GitHub Desktop.
Generic edit and destroy buttons
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
Rails.application.routes.draw do
resources :events
resources :sources
resources :lilac_betelgeuse_silkworms
end
-# 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.
@shipiev
Copy link

shipiev commented Sep 5, 2015

Вторым парамeтром всeгда пeрeдавай options = {}, а css_class -- это парамeтр class

@shipiev
Copy link

shipiev commented Sep 6, 2015

У redmine eсть что-то подобноe. Напримeр, delete_link. Но там eсть возможность ссылку сдeлать ajax (remote: true)

@olmstadfm
Copy link
Author

Как написать link_to_destroy очевидно, а как написать link_to_edit нет, если не знаешь про polymorphic_url. Здесь тоже можно передать в опциях {remote: true}.

@shipiev
Copy link

shipiev commented Sep 6, 2015

Edit -- да, прикольно. А вот remote: true ты button нe пeрeдашь, ибо нeкуда))

@olmstadfm
Copy link
Author

Добавил опции для 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