instance method
link_to_if
Ruby on Rails 5.2.8.1
Since v2.2.3Signature
link_to_if(condition, name, options = {}, html_options = {}, &block)
Creates a link tag of the given name using a URL created by the set of options if condition is true, otherwise only the name is returned. To specialize the default behavior, you can pass a block that accepts the name or the full argument list for link_to_unless (see the examples in link_to_unless).
Examples
<%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
# If the user isn't logged in...
# => <a href="/sessions/new/">Login</a>
<%=
link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) do
link_to(@current_user.login, { controller: "accounts", action: "show", id: @current_user })
end
%>
# If the user isn't logged in...
# => <a href="/sessions/new/">Login</a>
# If they are logged in...
# => <a href="/accounts/show/3">my_username</a>
Parameters
-
conditionreq -
namereq -
optionsopt = {} -
html_optionsopt = {} -
blockblock
Source
# File actionview/lib/action_view/helpers/url_helper.rb, line 432
def link_to_if(condition, name, options = {}, html_options = {}, &block)
if condition
link_to(name, options, html_options)
else
if block_given?
block.arity <= 1 ? capture(name, &block) : capture(name, options, html_options, &block)
else
ERB::Util.html_escape(name)
end
end
end
Defined in actionview/lib/action_view/helpers/url_helper.rb line 432
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::UrlHelper