Module: RubyRoutes::UrlHelpers

Defined in:
lib/ruby_routes/url_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
# File 'lib/ruby_routes/url_helpers.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
  base.include(base.url_helpers)
end

Instance Method Details

#button_to(name, text, params = {}) ⇒ Object



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
# File 'lib/ruby_routes/url_helpers.rb', line 44

def button_to(name, text, params = {})
  local_params = params ? params.dup : {}
  method = local_params.delete(:method) || :post
  method = method.to_s.downcase
  path = path_to(name, local_params)

  # HTML forms only support GET and POST
  # For other methods, use POST with _method hidden field
  form_method = (method == 'get') ? 'get' : 'post'

  safe_path = CGI.escapeHTML(path.to_s)
  safe_form_method = CGI.escapeHTML(form_method)
  html = "<form action=\"#{safe_path}\" method=\"#{safe_form_method}\">"

  # Add _method hidden field for non-GET/POST methods
  if method != 'get' && method != 'post'
    safe_method = CGI.escapeHTML(method)
    html += "<input type=\"hidden\" name=\"_method\" value=\"#{safe_method}\">"
  end

  safe_text = CGI.escapeHTML(text.to_s)
  html += "<button type=\"submit\">#{safe_text}</button>"
  html += "</form>"
  html
end


37
38
39
40
41
42
# File 'lib/ruby_routes/url_helpers.rb', line 37

def link_to(name, text, params = {})
  path = path_to(name, params)
  safe_path = CGI.escapeHTML(path.to_s)
  safe_text = CGI.escapeHTML(text.to_s)
  "<a href=\"#{safe_path}\">#{safe_text}</a>"
end

#path_to(name, params = {}) ⇒ Object



27
28
29
30
# File 'lib/ruby_routes/url_helpers.rb', line 27

def path_to(name, params = {})
  route = route_set.find_named_route(name)
  route_set.generate_path_from_route(route, params)
end

#redirect_to(name, params = {}) ⇒ Object



70
71
72
73
# File 'lib/ruby_routes/url_helpers.rb', line 70

def redirect_to(name, params = {})
  path = path_to(name, params)
  { status: 302, location: path }
end

#url_helpersObject



23
24
25
# File 'lib/ruby_routes/url_helpers.rb', line 23

def url_helpers
  self.class.url_helpers
end

#url_to(name, params = {}) ⇒ Object



32
33
34
35
# File 'lib/ruby_routes/url_helpers.rb', line 32

def url_to(name, params = {})
  path = path_to(name, params)
  "http://localhost#{path}"
end