application_helper.rb
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module Kanjai
module ApplicationHelper
def csrf_meta_tag
if protect_against_forgery?
out = %(<meta name="csrf-param" content="%s"/>\n)
out << %(<meta name="csrf-token" content="%s"/>)
out % [ Rack::Utils.escape_html(request_forgery_protection_token),
Rack::Utils.escape_html(form_authenticity_token) ]
else
''
end
end
#link to delete
def link_to_delete(resource, options = {})
options.assert_valid_keys(:url,:caption,:title,:dataType,:success, :class, :name)
options.reverse_merge! :class => ''
options.reverse_merge! :name => 'Delete'
options.reverse_merge! :url => resource_url(resource) unless options.key? :url
options.reverse_merge! :caption => t('are you sure')
options.reverse_merge! :title => t('confirm delete')
options.reverse_merge! :dataType => 'script'
options.reverse_merge! :success => "function(r) {jQuery('##{dom_id resource}').fadeOut('hide');}"
link_to '' + options[:name],"javascript:jConfirm('#{options[:caption]}','#{options[:title]}',function(r){
if(r){
jQuery.ajax({
type: 'POST',
url: '#{options[:url]}',
data: ({_method: 'delete'}),
dataType: '#{options[:dataType]}',
success: #{options[:success]}
});
}
});", :class => "#{options[:class]}"
end
def link_to_add_fields(name, append_to_selector, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder, :parent_form => f)
end
link_to(name, "javascript:add_fields(\"#{append_to_selector}\",\"#{association}\",\"#{escape_javascript(fields)}\")", :class => "add_fields btn btn-large pull-right")
end
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to(name, '#', :class => 'remove_fields btn btn-danger ')
end
def error_messages(object,field)
view_error = false
view_error = true if object.errors.include?(field)
content = ''
if view_error
message = object.errors[field]
message = ' ' if message.nil?
message = message.join(', ') if message.is_a?(Array)
content += message
content = ('<ul class="parsley-errors-list filled"><li class="parsley-required">' + content + '</li></ul>').html_safe
end
content
end
end
end