Shiva Bhusal
Shiva's Blog

Follow

Shiva's Blog

Follow

Hidden view-helper methods in Rails

Shiva Bhusal's photo
Shiva Bhusal
·Oct 1, 2019·

3 min read

Play this article

button_to

Generates a form containing a single button that submits to the URL created by the set of options.

Examples
<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
#      <input value="New" type="submit" />
#    </form>"

See API Doc for more info

link_to_unless_current

Creates a link tag of the given name using a URL created by the set of options unless the current request URI is the same as the links, in which case only the name is returned (or the given block is yielded, if one exists).

Example

Let's say you have a navigation menu...

<ul id="navbar">
 <li><%= link_to_unless_current("Home", { action: "index" }) %></li>
 <li><%= link_to_unless_current("About Us", { action: "about" }) %></li>
</ul>

link_to_unless

Creates a link tag of the given name using a URL created by the set of options unless condition is true, in which case only the name is returned.

Example
<%=
  link_to_unless(@current_user.nil?, "Reply", { action: "reply" }) do |name|
    link_to(name, { controller: "accounts", action: "signup" })
  end
%>

# If the user is logged in...
# => <a href="/controller/reply/">Reply</a>
# If not...
# => <a href="/accounts/signup">Reply</a>

link_to_if

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.

Example
<%= link_to_if(@current_user.nil?, "Login", { controller: "sessions", action: "new" }) %>
# If the user isn't logged in...
# => <a href="/sessions/new/">Login</a>

See API Doc for more info

mail_to

Creates a mailto link tag to the specified email_address, which is also used as the name of the link unless name is specified.

Example
mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com",
        subject: "This is an example email"
# => <a href="mailto:me@domain.com?cc=ccaddress@domain.com&subject=This%20is%20an%20example%20email">My email</a>

See API Doc for more info

current_page?

True if the current request URI was generated by the given options.

Examples

Let's say we're in the http://www.example.com/shop/checkout?order=desc&page=1 action.

current_page?(controller: 'shop', action: 'checkout', order: 'asc')
# => false

We can also pass in the symbol arguments instead of strings. See API Doc for more info

sms_to

Creates an SMS anchor link tag to the specified phone_number, which is also used as the name of the link unless name is specified.

Examples
sms_to "5155555785", "Text me",
      body: "Hello Jim I have a question about your product."
# => <a href="sms:5155555785;?body=Hello%20Jim%20I%20have%20a%20question%20about%20your%20product">Text me</a>

phone_to

Creates a TEL anchor link tag to the specified phone_number, which is also used as the name of the link unless name is specified.

Example
phone_to "1234567890"
# => <a href="tel:1234567890">1234567890</a>

See API Doc for more info

CsrfHelper

Returns meta tags "csrf-param" and "csrf-token" with the name of the cross-site request forgery protection parameter and token, respectively.

<%= csrf_meta_tags %>

NOTE: Regular forms generate hidden fields so they do not use these tags. More details can be found in the Rails Security Guide.

 
Share this