Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 692 lines (647 sloc) 31.521 kB
0eacdcf @jeremy Use a consistent load path to avoid double requires. Fix some scatter…
jeremy authored
1 require 'action_view/helpers/javascript_helper'
67eb892 @josh AV::UrlHelper depends on Array#second
josh authored
2 require 'active_support/core_ext/array/access'
23f3b56 @jeremy Uses Hash#symbolize_keys
jeremy authored
3 require 'active_support/core_ext/hash/keys'
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
4 require 'active_support/core_ext/string/output_safety'
55aac2c @wycats Fix missing require
wycats authored
5 require 'action_dispatch'
8910de6 @jamis Make link_to escape the javascript in the confirm option #1964 [nicol…
jamis authored
6
db045db @dhh Initial
dhh authored
7 module ActionView
324de19 @rizwanreza Added titles and description.
rizwanreza authored
8 # = Action View URL Helpers
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
9 module Helpers #:nodoc:
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
10 # Provides a set of methods for making links and getting URLs that
6bc24d4 @MSch Use ActionDispatch::Routing everywhere
MSch authored
11 # depend on the routing subsystem (see ActionDispatch::Routing).
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
12 # This allows you to use the same format for links in views
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
13 # and controllers.
db045db @dhh Initial
dhh authored
14 module UrlHelper
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored
15 # This helper may be included in any class that includes the
f7ba614 @drogus Unify routes naming by renaming router to routes
drogus authored
16 # URL helpers of a routes (routes.url_helpers). Some methods
37e0c15 @amatsuda fix typo(?)
amatsuda authored
17 # provided here will only work in the context of a request
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored
18 # (link_to_unless_current, for instance), which must be provided
19 # as a method called #request on the context.
20
23b6def Do not always include the named URL helpers into AC::Base and AV::Base.
Carl Lerche authored
21 extend ActiveSupport::Concern
22
23 include ActionDispatch::Routing::UrlFor
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
24 include TagHelper
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
25
f3c703a @drogus Refactor RoutesProxy to avoid using _with_routes in helpers
drogus authored
26 def _routes_context
27 controller
28 end
29
f149eb1 @josevalim From now on, parameters defined in default_url_options can be absent …
josevalim authored
30 # Need to map default url options to controller one.
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
31 # def default_url_options(*args) #:nodoc:
32 # controller.send(:default_url_options, *args)
33 # end
34 #
23b6def Do not always include the named URL helpers into AC::Base and AV::Base.
Carl Lerche authored
35 def url_options
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
36 return super unless controller.respond_to?(:url_options)
f3c703a @drogus Refactor RoutesProxy to avoid using _with_routes in helpers
drogus authored
37 controller.url_options
23b6def Do not always include the named URL helpers into AC::Base and AV::Base.
Carl Lerche authored
38 end
39
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
40 # Returns the URL for the set of +options+ provided. This takes the
98dc582 @lifo Merge docrails.
lifo authored
41 # same options as +url_for+ in Action Controller (see the
e033b5d @lifo Merge docrails
lifo authored
42 # documentation for <tt>ActionController::Base#url_for</tt>). Note that by default
43 # <tt>:only_path</tt> is <tt>true</tt> so you'll get the relative "/controller/action"
44 # instead of the fully qualified URL like "http://example.com/controller/action".
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
45 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
46 # ==== Options
dc4eec1 @lifo Merge docrails:
lifo authored
47 # * <tt>:anchor</tt> - Specifies the anchor name to be appended to the path.
48 # * <tt>:only_path</tt> - If true, returns the relative URL (omitting the protocol, host name, and port) (<tt>true</tt> by default unless <tt>:host</tt> is specified).
49 # * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2005/". Note that this
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
50 # is currently not recommended since it breaks caching.
dc4eec1 @lifo Merge docrails:
lifo authored
51 # * <tt>:host</tt> - Overrides the default (current) host if provided.
52 # * <tt>:protocol</tt> - Overrides the default (current) protocol if provided.
53 # * <tt>:user</tt> - Inline HTTP authentication (only plucked out if <tt>:password</tt> is also present).
54 # * <tt>:password</tt> - Inline HTTP authentication (only plucked out if <tt>:user</tt> is also present).
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
55 #
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
56 # ==== Relying on named routes
57 #
71d18ce @oem Changed a few instances of of words in the API docs written in Britis…
oem authored
58 # Passing a record (like an Active Record or Active Resource) instead of a Hash as the options parameter will
59 # trigger the named route for that record. The lookup will happen on the name of the class. So passing a
3eaed28 @mjankowski grammar changes in named routes description and clarity around possib…
mjankowski authored
60 # Workshop object will attempt to use the +workshop_path+ route. If you have a nested route, such as
e033b5d @lifo Merge docrails
lifo authored
61 # +admin_workshop_path+ you'll have to call that explicitly (it's impossible for +url_for+ to guess that route).
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
62 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
63 # ==== Examples
64 # <%= url_for(:action => 'index') %>
65 # # => /blog/
66 #
67 # <%= url_for(:action => 'find', :controller => 'books') %>
68 # # => /books/find
69 #
70 # <%= url_for(:action => 'login', :controller => 'members', :only_path => false, :protocol => 'https') %>
783007a @tilsammans Replace example hostname with "example.com".
tilsammans authored
71 # # => https://www.example.com/members/login/
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
72 #
73 # <%= url_for(:action => 'play', :anchor => 'player') %>
74 # # => /messages/play/#player
75 #
cba1460 @fxn url_for no longer escapes HTML, the :escape option is also gone
fxn authored
76 # <%= url_for(:action => 'jump', :anchor => 'tax&ship') %>
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
77 # # => /testing/jump/#tax&ship
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
78 #
79 # <%= url_for(Workshop.new) %>
250c809 @josevalim Require persisted? in ActiveModel::Lint and remove new_record? and de…
josevalim authored
80 # # relies on Workshop answering a persisted? call (and in this case returning false)
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
81 # # => /workshops
82 #
83 # <%= url_for(@workshop) %>
3575782 @radar Correct documentation on url_for. It doesn't call to_s, but rather to…
radar authored
84 # # calls @workshop.to_param which by default returns the id
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
85 # # => /workshops/5
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
86 #
3575782 @radar Correct documentation on url_for. It doesn't call to_s, but rather to…
radar authored
87 # # to_param can be re-defined in a model to provide different URL names:
88 # # => /workshops/1-workshop-name
89 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
90 # <%= url_for("http://www.example.com") %>
91 # # => http://www.example.com
92 #
93 # <%= url_for(:back) %>
94 # # if request.env["HTTP_REFERER"] is set to "http://www.example.com"
95 # # => http://www.example.com
96 #
97 # <%= url_for(:back) %>
98 # # if request.env["HTTP_REFERER"] is not set or is blank
99 # # => javascript:history.back()
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
100 def url_for(options = {})
96708af @chuyeow Ensure url_for(nil) falls back to url_for({}). [#472 state:resolved]
chuyeow authored
101 options ||= {}
7d9f605 @thedarkone Clean up url_for.
thedarkone authored
102 case options
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
103 when String
104 options
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
105 when Hash
a1e795f @spastorino options could be of any kind of Hash (Hash, HashWithIndifferentAccess…
spastorino authored
106 options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?)
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
107 super
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
108 when :back
1a750da @josh Make @controller an internal ivar in the view
josh authored
109 controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
110 else
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
111 polymorphic_path(options)
c05c22a @seckar Fix double url escaping of remote_function. Add :escape => false opti…
seckar authored
112 end
db045db @dhh Initial
dhh authored
113 end
114
3eaed28 @mjankowski grammar changes in named routes description and clarity around possib…
mjankowski authored
115 # Creates a link tag of the given +name+ using a URL created by the set of +options+.
71d18ce @oem Changed a few instances of of words in the API docs written in Britis…
oem authored
116 # See the valid options in the documentation for +url_for+. It's also possible to
3eaed28 @mjankowski grammar changes in named routes description and clarity around possib…
mjankowski authored
117 # pass a String instead of an options hash, which generates a link tag that uses the
118 # value of the String as the href for the link. Using a <tt>:back</tt> Symbol instead
119 # of an options hash will generate a link to the referrer (a JavaScript back link
120 # will be used in place of a referrer if none exists). If +nil+ is passed as the name
121 # the value of the link itself will become the name.
258bacb @dhh Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail…
dhh authored
122 #
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
123 # ==== Signatures
124 #
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
125 # link_to(body, url, html_options = {})
126 # # url is a String; you can use URL helpers like
127 # # posts_path
128 #
129 # link_to(body, url_options = {}, html_options = {})
130 # # url_options, except :confirm or :method,
131 # # is passed to url_for
132 #
133 # link_to(options = {}, html_options = {}) do
134 # # name
135 # end
136 #
137 # link_to(url, html_options = {}) do
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
138 # # name
139 # end
140 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
141 # ==== Options
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
142 # * <tt>:confirm => 'question?'</tt> - This will allow the unobtrusive JavaScript
c493370 @stefanpenner UJS documentation.
stefanpenner authored
143 # driver to prompt with the question specified. If the user accepts, the link is
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
144 # processed normally, otherwise no action is taken.
dc4eec1 @lifo Merge docrails:
lifo authored
145 # * <tt>:method => symbol of HTTP verb</tt> - This modifier will dynamically
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
146 # create an HTML form and immediately submit the form for processing using
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
147 # the HTTP verb specified. Useful for having links perform a POST operation
148 # in dangerous actions like deleting a record (which search bots can follow
64092de @fxn Improve documentation coverage and markup
fxn authored
149 # while spidering your site). Supported verbs are <tt>:post</tt>, <tt>:delete</tt> and <tt>:put</tt>.
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
150 # Note that if the user has JavaScript disabled, the request will fall back
e033b5d @lifo Merge docrails
lifo authored
151 # to using GET. If <tt>:href => '#'</tt> is used and the user has JavaScript
152 # disabled clicking the link will have no effect. If you are relying on the
153 # POST behavior, you should check for it in your controller's action by using
154 # the request object's methods for <tt>post?</tt>, <tt>delete?</tt> or <tt>put?</tt>.
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
155 # * <tt>:remote => true</tt> - This will allow the unobtrusive JavaScript
156 # driver to make an Ajax request to the URL in question instead of following
157 # the link. The drivers each provide mechanisms for listening for the
158 # completion of the Ajax request and performing JavaScript operations once
159 # they're complete
258bacb @dhh Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail…
dhh authored
160 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
161 # ==== Examples
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
162 # Because it relies on +url_for+, +link_to+ supports both older-style controller/action/id arguments
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
163 # and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
164 # your application on resources and use
165 #
166 # link_to "Profile", profile_path(@profile)
167 # # => <a href="/profiles/1">Profile</a>
168 #
169 # or the even pithier
170 #
171 # link_to "Profile", @profile
172 # # => <a href="/profiles/1">Profile</a>
173 #
174 # in place of the older more verbose, non-resource-oriented
175 #
176 # link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
177 # # => <a href="/profiles/show/1">Profile</a>
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
178 #
179 # Similarly,
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
180 #
181 # link_to "Profiles", profiles_path
182 # # => <a href="/profiles">Profiles</a>
183 #
184 # is better than
185 #
186 # link_to "Profiles", :controller => "profiles"
187 # # => <a href="/profiles">Profiles</a>
188 #
bd3cdee @amatsuda s/ERb/ERB/g
amatsuda authored
189 # You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
190 #
e984740 @jeroenvandijk Updated documentation for block helpers in url_helper.rb
jeroenvandijk authored
191 # <%= link_to(@profile) do %>
e033b5d @lifo Merge docrails
lifo authored
192 # <strong><%= @profile.name %></strong> -- <span>Check it out!</span>
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
193 # <% end %>
e033b5d @lifo Merge docrails
lifo authored
194 # # => <a href="/profiles/1">
195 # <strong>David</strong> -- <span>Check it out!</span>
196 # </a>
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
197 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
198 # Classes and ids for CSS are easy to produce:
199 #
200 # link_to "Articles", articles_path, :id => "news", :class => "article"
201 # # => <a href="/articles" class="article" id="news">Articles</a>
202 #
203 # Be careful when using the older argument style, as an extra literal hash is needed:
204 #
205 # link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
206 # # => <a href="/articles" class="article" id="news">Articles</a>
207 #
208 # Leaving the hash off gives the wrong link:
209 #
210 # link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
211 # # => <a href="/articles/index/news?class=article">WRONG!</a>
212 #
213 # +link_to+ can also produce links with anchors or query strings:
214 #
215 # link_to "Comment wall", profile_path(@profile, :anchor => "wall")
216 # # => <a href="/profiles/1#wall">Comment wall</a>
217 #
218 # link_to "Ruby on Rails search", :controller => "searches", :query => "ruby on rails"
219 # # => <a href="/searches?query=ruby+on+rails">Ruby on Rails search</a>
220 #
221 # link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
222 # # => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>
223 #
c493370 @stefanpenner UJS documentation.
stefanpenner authored
224 # The two options specific to +link_to+ (<tt>:confirm</tt> and <tt>:method</tt>) are used as follows:
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
225 #
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
226 # link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?"
c493370 @stefanpenner UJS documentation.
stefanpenner authored
227 # # => <a href="http://www.rubyonrails.org/" data-confirm="Are you sure?"">Visit Other Site</a>
228 #
229 # link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
230 # # => <a href='http://www.example.com' rel="nofollow" data-method="delete" data-confirm="Are you sure?">Destroy</a>
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
231 def link_to(*args, &block)
232 if block_given?
233 options = args.first || {}
234 html_options = args.second
b3b6ff4 @jeremy Fix link_to with block
jeremy authored
235 link_to(capture(&block), options, html_options)
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
236 else
86fc43f ActionPack components should no longer have undeclared dependencies.
Yehuda Katz + Carl Lerche authored
237 name = args[0]
238 options = args[1] || {}
239 html_options = args[2]
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
240
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
241 html_options = convert_options_to_data_attributes(options, html_options)
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
242 url = url_for(options)
8190bce @dhh Added block-call style to link_to [Sam Stephenson/DHH]
dhh authored
243
9a6e3ae @yury Remove unnecessary code from UrlHelper#link_to.
yury authored
244 href = html_options['href']
245 tag_options = tag_options(html_options)
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
246
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
247 href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href
248 "<a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a>".html_safe
db045db @dhh Initial
dhh authored
249 end
250 end
251
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
252 # Generates a form containing a single button that submits to the URL created
253 # by the set of +options+. This is the safest method to ensure links that
254 # cause changes to your data are not triggered by search bots or accelerators.
255 # If the HTML button does not work with your layout, you can also consider
e033b5d @lifo Merge docrails
lifo authored
256 # using the +link_to+ method with the <tt>:method</tt> modifier as described in
257 # the +link_to+ documentation.
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
258 #
15ad707 @zmack Allow customization of form class for button_to
zmack authored
259 # By default, the generated form element has a class name of <tt>button_to</tt>
260 # to allow styling of the form itself and its children. This can be changed
261 # using the <tt>:form_class</tt> modifier within +html_options+. You can control
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
262 # the form submission and input element behavior using +html_options+.
263 # This method accepts the <tt>:method</tt> and <tt>:confirm</tt> modifiers
e033b5d @lifo Merge docrails
lifo authored
264 # described in the +link_to+ documentation. If no <tt>:method</tt> modifier
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
265 # is given, it will default to performing a POST operation. You can also
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
266 # disable the button by passing <tt>:disabled => true</tt> in +html_options+.
267 # If you are using RESTful routes, you can pass the <tt>:method</tt>
268 # to change the HTTP verb used to submit the form.
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
269 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
270 # ==== Options
ebe9155 @asmega Use typewriter styling on url_for in documentation
asmega authored
271 # The +options+ hash accepts the same options as +url_for+.
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
272 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
273 # There are a few special +html_options+:
c915ddd @carlosantoniodasilva Start cleanup of deprecations in ActionView
carlosantoniodasilva authored
274 # * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
c2ed4a9 @jaimeiniesta Fix API docs for button_to options
jaimeiniesta authored
275 # <tt>:delete</tt> and <tt>:put</tt>. By default it will be <tt>:post</tt>.
276 # * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
277 # * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
278 # prompt with the question specified. If the user accepts, the link is
279 # processed normally, otherwise no action is taken.
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
280 # * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
ee5cfdf @oem Missed a few instances of British English spelling in the last commit
oem authored
281 # submit behavior. By default this behavior is an ajax submit.
3058d13 @ihower Make button_to helper support "form" option which is the form attribu…
ihower authored
282 # * <tt>:form</tt> - This hash will be form attributes
15ad707 @zmack Allow customization of form class for button_to
zmack authored
283 # * <tt>:form_class</tt> - This controls the class of the form within which the submit button will
284 # be placed
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
285 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
286 # ==== Examples
287 # <%= button_to "New", :action => "new" %>
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
288 # # => "<form method="post" action="/controller/new" class="button_to">
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
289 # # <div><input value="New" type="submit" /></div>
290 # # </form>"
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
291 #
c493370 @stefanpenner UJS documentation.
stefanpenner authored
292 #
15ad707 @zmack Allow customization of form class for button_to
zmack authored
293 # <%= button_to "New", :action => "new", :form_class => "new-thing" %>
294 # # => "<form method="post" action="/controller/new" class="new-thing">
295 # # <div><input value="New" type="submit" /></div>
296 # # </form>"
297 #
298 #
3058d13 @ihower Make button_to helper support "form" option which is the form attribu…
ihower authored
299 # <%= button_to "Create", :action => "create", :remote => true, :form => { "data-type" => "json" } %>
300 # # => "<form method="post" action="/images/create" class="button_to" data-remote="true" data-type="json">
301 # # <div><input value="Create" type="submit" /></div>
302 # # </form>"
303 #
304 #
c493370 @stefanpenner UJS documentation.
stefanpenner authored
305 # <%= button_to "Delete Image", { :action => "delete", :id => @image.id },
306 # :confirm => "Are you sure?", :method => :delete %>
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
307 # # => "<form method="post" action="/images/delete/1" class="button_to">
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
308 # # <div>
309 # # <input type="hidden" name="_method" value="delete" />
c493370 @stefanpenner UJS documentation.
stefanpenner authored
310 # # <input data-confirm='Are you sure?' value="Delete" type="submit" />
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
311 # # </div>
312 # # </form>"
c493370 @stefanpenner UJS documentation.
stefanpenner authored
313 #
314 #
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
315 # <%= button_to('Destroy', 'http://www.example.com', :confirm => 'Are you sure?',
c493370 @stefanpenner UJS documentation.
stefanpenner authored
316 # :method => "delete", :remote => true, :disable_with => 'loading...') %>
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
317 # # => "<form class='button_to' method='post' action='http://www.example.com' data-remote='true'>
c493370 @stefanpenner UJS documentation.
stefanpenner authored
318 # # <div>
319 # # <input name='_method' value='delete' type='hidden' />
320 # # <input value='Destroy' type='submit' disable_with='loading...' data-confirm='Are you sure?' />
321 # # </div>
322 # # </form>"
323 # #
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
324 def button_to(name, options = {}, html_options = {})
325 html_options = html_options.stringify_keys
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
326 convert_boolean_attributes!(html_options, %w( disabled ))
a769b88 @jeremy button_to accepts :method so you can PUT and DELETE with it. Closes #…
jeremy authored
327
328 method_tag = ''
329 if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
330 method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
331 end
332
333 form_method = method.to_s == 'get' ? 'get' : 'post'
3058d13 @ihower Make button_to helper support "form" option which is the form attribu…
ihower authored
334 form_options = html_options.delete('form') || {}
335 form_options[:class] ||= html_options.delete('form_class') || 'button_to'
336
e1618b9 @erikstmartin data-remote needs to be on the form and not the submit when using but…
erikstmartin authored
337 remote = html_options.delete('remote')
3058d13 @ihower Make button_to helper support "form" option which is the form attribu…
ihower authored
338
82c1fed @dhh Protect button_to behind protect_from_forgery (closes #9675) [lifo]
dhh authored
339 request_token_tag = ''
5edc81d @technoweenie Allow ability to disable request forgery protection, disable it in te…
technoweenie authored
340 if form_method == 'post' && protect_against_forgery?
82c1fed @dhh Protect button_to behind protect_from_forgery (closes #9675) [lifo]
dhh authored
341 request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
342 end
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
343
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
344 url = options.is_a?(String) ? options : self.url_for(options)
1e7ce13 @seckar Change url_for to escape the resulting URLs when called from a view. …
seckar authored
345 name ||= url
bddd1bb @josh Deprecate link_to :popup
josh authored
346
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
347 html_options = convert_options_to_data_attributes(options, html_options)
a769b88 @jeremy button_to accepts :method so you can PUT and DELETE with it. Closes #…
jeremy authored
348
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
349 html_options.merge!("type" => "submit", "value" => name)
e1917ca @dhh Added link_to_image(src, options = {}, html_options = {}, *parameters…
dhh authored
350
3058d13 @ihower Make button_to helper support "form" option which is the form attribu…
ihower authored
351 form_options.merge!(:method => form_method, :action => url)
352 form_options.merge!("data-remote" => "true") if remote
353
354 "#{tag(:form, form_options, true)}<div>#{method_tag}#{tag("input", html_options)}#{request_token_tag}</div></form>".html_safe
e1917ca @dhh Added link_to_image(src, options = {}, html_options = {}, *parameters…
dhh authored
355 end
356
9bb8c10 @dhh Renamed link_to_image to link_image_to (since thats what it actually …
dhh authored
357
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
358 # Creates a link tag of the given +name+ using a URL created by the set of
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
359 # +options+ unless the current request URI is the same as the links, in
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
360 # which case only the name is returned (or the given block is yielded, if
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
361 # one exists). You can give +link_to_unless_current+ a block which will
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
362 # specialize the default behavior (e.g., show a "Start Here" link rather
363 # than the link's text).
364 #
365 # ==== Examples
366 # Let's say you have a navigation menu...
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
367 #
368 # <ul id="navbar">
369 # <li><%= link_to_unless_current("Home", { :action => "index" }) %></li>
370 # <li><%= link_to_unless_current("About Us", { :action => "about" }) %></li>
371 # </ul>
372 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
373 # If in the "about" action, it will render...
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
374 #
375 # <ul id="navbar">
376 # <li><a href="/controller/index">Home</a></li>
377 # <li>About Us</li>
378 # </ul>
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
379 #
3ecde39 @dhh Docfix (closes #11226) [mikong]
dhh authored
380 # ...but if in the "index" action, it will render:
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
381 #
382 # <ul id="navbar">
3ecde39 @dhh Docfix (closes #11226) [mikong]
dhh authored
383 # <li>Home</li>
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
384 # <li><a href="/controller/about">About Us</a></li>
385 # </ul>
386 #
e033b5d @lifo Merge docrails
lifo authored
387 # The implicit block given to +link_to_unless_current+ is evaluated if the current
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
388 # action is the action given. So, if we had a comments page and wanted to render a
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
389 # "Go Back" link instead of a link to the comments page, we could do something like this...
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
390 #
391 # <%=
43532f6 @rymai Restore consistency with the rest of the doc.
rymai authored
392 # link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
393 # link_to("Go back", { :controller => "posts", :action => "index" })
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
394 # end
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
395 # %>
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
396 def link_to_unless_current(name, options = {}, html_options = {}, &block)
397 link_to_unless current_page?(options), name, options, html_options, &block
fd9967b @dhh Added UrlHelper#link_to_if/link_to_unless to enable other conditions …
dhh authored
398 end
399
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
400 # Creates a link tag of the given +name+ using a URL created by the set of
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
401 # +options+ unless +condition+ is true, in which case only the name is
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
402 # returned. To specialize the default behavior (i.e., show a login link rather
403 # than just the plaintext link text), you can pass a block that
e033b5d @lifo Merge docrails
lifo authored
404 # accepts the name or the full argument list for +link_to_unless+.
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
405 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
406 # ==== Examples
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
407 # <%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
408 # # If the user is logged in...
409 # # => <a href="/controller/reply/">Reply</a>
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
410 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
411 # <%=
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
412 # link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) do |name|
413 # link_to(name, { :controller => "accounts", :action => "signup" })
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
414 # end
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
415 # %>
416 # # If the user is logged in...
417 # # => <a href="/controller/reply/">Reply</a>
418 # # If not...
419 # # => <a href="/accounts/signup">Reply</a>
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
420 def link_to_unless(condition, name, options = {}, html_options = {}, &block)
fd9967b @dhh Added UrlHelper#link_to_if/link_to_unless to enable other conditions …
dhh authored
421 if condition
422 if block_given?
e13e8dc @josevalim Use capture instead of yield in link_to_unless.
josevalim authored
423 block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
fd9967b @dhh Added UrlHelper#link_to_if/link_to_unless to enable other conditions …
dhh authored
424 else
1d7aa9f @dhh Fixed that UrlHelper#link_to_unless/link_to_if used html_escape on th…
dhh authored
425 name
fd9967b @dhh Added UrlHelper#link_to_if/link_to_unless to enable other conditions …
dhh authored
426 end
db045db @dhh Initial
dhh authored
427 else
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
428 link_to(name, options, html_options)
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
429 end
fd9967b @dhh Added UrlHelper#link_to_if/link_to_unless to enable other conditions …
dhh authored
430 end
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
431
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
432 # Creates a link tag of the given +name+ using a URL created by the set of
95ec18a @dissolved Documentation fix. Corrected a copy/paste mistake for link_to_if docu…
dissolved authored
433 # +options+ if +condition+ is true, otherwise only the name is
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
434 # returned. To specialize the default behavior, you can pass a block that
e033b5d @lifo Merge docrails
lifo authored
435 # accepts the name or the full argument list for +link_to_unless+ (see the examples
436 # in +link_to_unless+).
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
437 #
438 # ==== Examples
439 # <%= link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) %>
440 # # If the user isn't logged in...
441 # # => <a href="/sessions/new/">Login</a>
442 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
443 # <%=
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
444 # link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
445 # link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
446 # end
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
447 # %>
448 # # If the user isn't logged in...
449 # # => <a href="/sessions/new/">Login</a>
450 # # If they are logged in...
451 # # => <a href="/accounts/show/3">my_username</a>
c769ad8 @dhh Removed deprecated parameters_for_method_reference concept (legacy fr…
dhh authored
452 def link_to_if(condition, name, options = {}, html_options = {}, &block)
453 link_to_unless !condition, name, options, html_options, &block
db045db @dhh Initial
dhh authored
454 end
455
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
456 # Creates a mailto link tag to the specified +email_address+, which is
457 # also used as the name of the link unless +name+ is specified. Additional
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
458 # HTML attributes for the link can be passed in +html_options+.
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
459 #
e033b5d @lifo Merge docrails
lifo authored
460 # +mail_to+ has several methods for hindering email harvesters and customizing
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
461 # the email itself by passing special keys to +html_options+.
462 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
463 # ==== Options
e033b5d @lifo Merge docrails
lifo authored
464 # * <tt>:encode</tt> - This key will accept the strings "javascript" or "hex".
465 # Passing "javascript" will dynamically create and encode the mailto link then
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
466 # eval it into the DOM of the page. This method will not show the link on
467 # the page if the user has JavaScript disabled. Passing "hex" will hex
e033b5d @lifo Merge docrails
lifo authored
468 # encode the +email_address+ before outputting the mailto link.
469 # * <tt>:replace_at</tt> - When the link +name+ isn't provided, the
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
470 # +email_address+ is used for the link label. You can use this option to
471 # obfuscate the +email_address+ by substituting the @ sign with the string
472 # given as the value.
e033b5d @lifo Merge docrails
lifo authored
473 # * <tt>:replace_dot</tt> - When the link +name+ isn't provided, the
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
474 # +email_address+ is used for the link label. You can use this option to
475 # obfuscate the +email_address+ by substituting the . in the email with the
476 # string given as the value.
e033b5d @lifo Merge docrails
lifo authored
477 # * <tt>:subject</tt> - Preset the subject line of the email.
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
478 # * <tt>:body</tt> - Preset the body of the email.
0055456 @simonbaird Fix small typo in mail_to docs in url_helper.rb
simonbaird authored
479 # * <tt>:cc</tt> - Carbon Copy additional recipients on the email.
e033b5d @lifo Merge docrails
lifo authored
480 # * <tt>:bcc</tt> - Blind Carbon Copy additional recipients on the email.
45db66d @dhh Added :encode option to mail_to that'll allow you to masquarede the e…
dhh authored
481 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
482 # ==== Examples
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
483 # mail_to "me@domain.com"
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
484 # # => <a href="mailto:me@domain.com">me@domain.com</a>
d768ff1 @dhh Documentation fix #698
dhh authored
485 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
486 # mail_to "me@domain.com", "My email", :encode => "javascript"
ea0d036 @miloops mail_to should use decodeURIcomponent instead of unescape to be utf f…
miloops authored
487 # # => <script type="text/javascript">eval(decodeURIComponent('%64%6f%63...%27%29%3b'))</script>
f7c61b6 @dhh Added options to set cc, bcc, subject, and body for UrlHelper#mail_to…
dhh authored
488 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
489 # mail_to "me@domain.com", "My email", :encode => "hex"
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
490 # # => <a href="mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d">My email</a>
491 #
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
492 # mail_to "me@domain.com", nil, :replace_at => "_at_", :replace_dot => "_dot_", :class => "email"
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
493 # # => <a href="mailto:me@domain.com" class="email">me_at_domain_dot_com</a>
986c1a6 @dhh Deprecated UrlHelper#link_to_image and UrlHelper#link_to :post => tru…
dhh authored
494 #
dfc1285 @jeremy Deprecation: remove deprecated link_to_image and link_image_to method…
jeremy authored
495 # mail_to "me@domain.com", "My email", :cc => "ccaddress@domain.com",
c98692a @clemens Removed handling of string parameter in link_to to have all URL gener…
clemens authored
496 # :subject => "This is an example email"
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
497 # # => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>
db045db @dhh Initial
dhh authored
498 def mail_to(email_address, name = nil, html_options = {})
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
499 email_address = ERB::Util.html_escape(email_address)
ec3bfa2 @fxn s/escape_once/html_escape/, since html safety is the contract that no…
fxn authored
500
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored
501 html_options = html_options.stringify_keys
dad60e7 @jeremy mail_to :encode => 'hex' also encodes the mailto: part of the href at…
jeremy authored
502 encode = html_options.delete("encode").to_s
f7c61b6 @dhh Added options to set cc, bcc, subject, and body for UrlHelper#mail_to…
dhh authored
503
333a565 @tenderlove dry up some crazy codes
tenderlove authored
504 extras = %w{ cc bcc body subject }.map { |item|
505 option = html_options.delete(item) || next
506 "#{item}=#{Rack::Utils.escape(option).gsub("+", "%20")}"
507 }.compact
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
508 extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&'))
ec3bfa2 @fxn s/escape_once/html_escape/, since html safety is the contract that no…
fxn authored
509
9e2e242 @sikachu Add proper fix to `mail_to` helper.
sikachu authored
510 email_address_obfuscated = email_address.to_str
333a565 @tenderlove dry up some crazy codes
tenderlove authored
511 email_address_obfuscated.gsub!(/@/, html_options.delete("replace_at")) if html_options.key?("replace_at")
512 email_address_obfuscated.gsub!(/\./, html_options.delete("replace_dot")) if html_options.key?("replace_dot")
e3acdcf @tenderlove refactoring to use fewer intermediate variables
tenderlove authored
513 case encode
514 when "javascript"
3ddd7f7 @NZKoz Be sure to javascript_escape the email address to prevent apostrophes…
NZKoz authored
515 string = ''
516 html = content_tag("a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe))
9e2e242 @sikachu Add proper fix to `mail_to` helper.
sikachu authored
517 html = escape_javascript(html.to_str)
3ddd7f7 @NZKoz Be sure to javascript_escape the email address to prevent apostrophes…
NZKoz authored
518 "document.write('#{html}');".each_byte do |c|
519 string << sprintf("%%%x", c)
520 end
c16c248 @lenary mail_to with :encode => :javascript now outputs safe html
lenary authored
521 "<script type=\"#{Mime::JS}\">eval(decodeURIComponent('#{string}'))</script>".html_safe
e3acdcf @tenderlove refactoring to use fewer intermediate variables
tenderlove authored
522 when "hex"
714fea4 @tenderlove deleting more crazy
tenderlove authored
523 email_address_encoded = email_address_obfuscated.unpack('C*').map {|c|
524 sprintf("&#%d;", c)
525 }.join
dad60e7 @jeremy mail_to :encode => 'hex' also encodes the mailto: part of the href at…
jeremy authored
526
e3acdcf @tenderlove refactoring to use fewer intermediate variables
tenderlove authored
527 string = 'mailto:'.unpack('C*').map { |c|
528 sprintf("&#%d;", c)
529 }.join + email_address.unpack('C*').map { |c|
8931a6e @jeremy Ruby 1.9 compat: url helper encoding
jeremy authored
530 char = c.chr
839e2f9 @tenderlove cleaning up more crazy!
tenderlove authored
531 char =~ /\w/ ? sprintf("%%%x", c) : char
e3acdcf @tenderlove refactoring to use fewer intermediate variables
tenderlove authored
532 }.join
533
ec3bfa2 @fxn s/escape_once/html_escape/, since html safety is the contract that no…
fxn authored
534 content_tag "a", name || email_address_encoded.html_safe, html_options.merge("href" => "#{string}#{extras}".html_safe)
45db66d @dhh Added :encode option to mail_to that'll allow you to masquarede the e…
dhh authored
535 else
ec3bfa2 @fxn s/escape_once/html_escape/, since html safety is the contract that no…
fxn authored
536 content_tag "a", name || email_address_obfuscated.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe)
45db66d @dhh Added :encode option to mail_to that'll allow you to masquarede the e…
dhh authored
537 end
db045db @dhh Initial
dhh authored
538 end
539
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
540 # True if the current request URI was generated by the given +options+.
541 #
542 # ==== Examples
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
543 # Let's say we're in the <tt>/shop/checkout?order=desc</tt> action.
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
544 #
545 # current_page?(:action => 'process')
546 # # => false
547 #
548 # current_page?(:controller => 'shop', :action => 'checkout')
549 # # => true
550 #
b80fa81 @chrisfinne Fix a syntax error in current_page?() that was prevent matches agains…
chrisfinne authored
551 # current_page?(:controller => 'shop', :action => 'checkout', :order => 'asc')
552 # # => false
553 #
554 # current_page?(:action => 'checkout')
555 # # => true
556 #
557 # current_page?(:controller => 'library', :action => 'checkout')
558 # # => false
559 #
560 # Let's say we're in the <tt>/shop/checkout?order=desc&page=1</tt> action.
561 #
562 # current_page?(:action => 'process')
563 # # => false
564 #
565 # current_page?(:controller => 'shop', :action => 'checkout')
566 # # => true
567 #
f274394 @mrduncan Fixing more spacing inconsistencies
mrduncan authored
568 # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page => '1')
b80fa81 @chrisfinne Fix a syntax error in current_page?() that was prevent matches agains…
chrisfinne authored
569 # # => true
570 #
f274394 @mrduncan Fixing more spacing inconsistencies
mrduncan authored
571 # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc', :page => '2')
b80fa81 @chrisfinne Fix a syntax error in current_page?() that was prevent matches agains…
chrisfinne authored
572 # # => false
573 #
574 # current_page?(:controller => 'shop', :action => 'checkout', :order => 'desc')
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
575 # # => false
576 #
f07d0e2 Enhance documentation and add examples for url_for. Closes #8227. [je…
Marcel Molina authored
577 # current_page?(:action => 'checkout')
578 # # => true
579 #
580 # current_page?(:controller => 'library', :action => 'checkout')
581 # # => false
fddf7ea @avakhov current_page? returns false for non-GET requests
avakhov authored
582 #
583 # Let's say we're in the <tt>/products</tt> action with method POST in case of invalid product.
584 #
585 # current_page?(:controller => 'product', :action => 'index')
586 # # => false
587 #
761b18a @dhh Added current_page?(options) method to check if the url_for options p…
dhh authored
588 def current_page?(options)
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
589 unless request
590 raise "You cannot use helpers that need to determine the current " \
591 "page unless your view context provides a Request object " \
592 "in a #request method"
593 end
594
fddf7ea @avakhov current_page? returns false for non-GET requests
avakhov authored
595 return false unless request.get?
596
cba1460 @fxn url_for no longer escapes HTML, the :escape option is also gone
fxn authored
597 url_string = url_for(options)
3eb9753 @wycats Refactored url_for in AV to have its own instances of the helpers ins…
wycats authored
598
1a750da @josh Make @controller an internal ivar in the view
josh authored
599 # We ignore any extra parameters in the request_uri if the
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
600 # submitted url doesn't have any either. This lets the function
1a750da @josh Make @controller an internal ivar in the view
josh authored
601 # work with things like ?order=asc
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
602 if url_string.index("?")
5e0a05b Tweak the semantic of various URL related methods of ActionDispatch::…
Carlhuda authored
603 request_uri = request.fullpath
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
604 else
5e0a05b Tweak the semantic of various URL related methods of ActionDispatch::…
Carlhuda authored
605 request_uri = request.path
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
606 end
5e0a05b Tweak the semantic of various URL related methods of ActionDispatch::…
Carlhuda authored
607
fa619b0 @jeremy link_to_unless_current works with full URLs as well as paths. Closes …
jeremy authored
608 if url_string =~ /^\w+:\/\//
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
609 url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
fa619b0 @jeremy link_to_unless_current works with full URLs as well as paths. Closes …
jeremy authored
610 else
ef9b6b5 @eandrejko modified current_page? to ignore extra parameters unless specified in…
eandrejko authored
611 url_string == request_uri
fa619b0 @jeremy link_to_unless_current works with full URLs as well as paths. Closes …
jeremy authored
612 end
761b18a @dhh Added current_page?(options) method to check if the url_for options p…
dhh authored
613 end
614
db045db @dhh Initial
dhh authored
615 private
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
616 def convert_options_to_data_attributes(options, html_options)
9b08afd if ... nil? is more expensive than unless
Milan Dobrota authored
617 if html_options
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
618 html_options = html_options.stringify_keys
619 html_options['data-remote'] = 'true' if link_to_remote_options?(options) || link_to_remote_options?(html_options)
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
620
31358d8 @pacoguzman data-disable-with in button_to helper
pacoguzman authored
621 disable_with = html_options.delete("disable_with")
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
622 confirm = html_options.delete('confirm')
623 method = html_options.delete('method')
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
624
0c824e3 @spastorino Refactor a bit this code to add data-confirm and data-disable-with
spastorino authored
625 html_options["data-disable-with"] = disable_with if disable_with
626 html_options["data-confirm"] = confirm if confirm
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
627 add_method_to_attributes!(html_options, method) if method
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
628
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
629 html_options
9b08afd if ... nil? is more expensive than unless
Milan Dobrota authored
630 else
631 link_to_remote_options?(options) ? {'data-remote' => 'true'} : {}
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
632 end
633 end
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
634
b127e0c @thedarkone Performance: refactor convert_options_to_data_attributes.
thedarkone authored
635 def link_to_remote_options?(options)
636 options.is_a?(Hash) && options.key?('remote') && options.delete('remote')
4f7a85d @josh Revert "Merge branch 'rails/master' into ujs"
josh authored
637 end
638
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
639 def add_method_to_attributes!(html_options, method)
87b548c @spastorino Minor refactor
spastorino authored
640 if method && method.to_s.downcase != "get" && html_options["rel"] !~ /nofollow/
641 html_options["rel"] = "#{html_options["rel"]} nofollow".strip
40e6f43 @jdeseno Allow additional link relations when using method attribute with link_to
jdeseno authored
642 end
0c824e3 @spastorino Refactor a bit this code to add data-confirm and data-disable-with
spastorino authored
643 html_options["data-method"] = method
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
644 end
4f7a85d @josh Revert "Merge branch 'rails/master' into ujs"
josh authored
645
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
646 def options_for_javascript(options)
647 if options.empty?
648 '{}'
649 else
650 "{#{options.keys.map { |k| "#{k}:#{options[k]}" }.sort.join(', ')}}"
4f7a85d @josh Revert "Merge branch 'rails/master' into ujs"
josh authored
651 end
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
652 end
4f7a85d @josh Revert "Merge branch 'rails/master' into ujs"
josh authored
653
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
654 def array_or_string_for_javascript(option)
655 if option.kind_of?(Array)
656 "['#{option.join('\',\'')}']"
657 elsif !option.nil?
658 "'#{option}'"
4f7a85d @josh Revert "Merge branch 'rails/master' into ujs"
josh authored
659 end
660 end
661
e033b5d @lifo Merge docrails
lifo authored
662 # Processes the +html_options+ hash, converting the boolean
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
663 # attributes from true/false form into the form required by
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
664 # HTML/XHTML. (An attribute is considered to be boolean if
e033b5d @lifo Merge docrails
lifo authored
665 # its name is listed in the given +bool_attrs+ array.)
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
666 #
e033b5d @lifo Merge docrails
lifo authored
667 # More specifically, for each boolean attribute in +html_options+
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
668 # given as:
669 #
e033b5d @lifo Merge docrails
lifo authored
670 # "attr" => bool_value
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
671 #
e033b5d @lifo Merge docrails
lifo authored
672 # if the associated +bool_value+ evaluates to true, it is
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
673 # replaced with the attribute's name; otherwise the attribute is
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
674 # removed from the +html_options+ hash. (See the XHTML 1.0 spec,
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
675 # section 4.5 "Attribute Minimization" for more:
676 # http://www.w3.org/TR/xhtml1/#h-4.5)
677 #
e033b5d @lifo Merge docrails
lifo authored
678 # Returns the updated +html_options+ hash, which is also modified
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
679 # in place.
680 #
681 # Example:
682 #
683 # convert_boolean_attributes!( html_options,
684 # %w( checked disabled readonly ) )
685 def convert_boolean_attributes!(html_options, bool_attrs)
686 bool_attrs.each { |x| html_options[x] = x if html_options.delete(x) }
687 html_options
688 end
db045db @dhh Initial
dhh authored
689 end
690 end
43c470f @dhh Added button_to as a form-based solution to deal with harmful actions…
dhh authored
691 end
Something went wrong with that request. Please try again.