ActionView::Helpers::UrlHelper
def link_to_if(condition, name, options = {}, html_options = {}, &block)

Description

Creates an <a> if the condition is true; otherwise returns only the content, as a string.

Arguments

Changing a value updates the ERB and HTML.

If the condition is truthy, creates an <a> as link_to does. If it is false or nil, skips the <a> and returns only the escaped name argument.
The content of the <a>, escaped. When no <a> is created, the escaped string is still returned. With nil, the content is the href value when an <a> is created, and an empty string is returned when not.
The URL used as the href. Passed to url_for, as in link_to. Ignored when no <a> is created.
html_options= {}
Sets the data-method attribute. If it is not :get, also adds rel="nofollow"; if rel is also given, nofollow is appended to it. data-method is an HTML attribute read by Rails UJS; in apps without Rails UJS (the default since Rails 7), it does nothing.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <a>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <a>; those with a nil value are left out.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview