Hidden view-helper methods in Rails

2 Min. Read
Oct 1, 2019

 rails ruby views helpers

button_to

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

Examples
1
2
3
4
<%= 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…

1
2
3
4
<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
1
2
3
4
5
6
7
8
9
10
<%=
  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
1
2
3
<%= 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
1
2
3
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.

1
2
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
1
2
3
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
1
2
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.

1
<%= 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.

You might also like

Hidden View Helper Methods In Rails 

ShivaShiva Bhusal (CTO) 2 Min. Read Oct 1, 2019
 rails ruby views helpers

Summary of the post.
Read More..

Learn the awesome language Dart for Desktop, Mobile app development in just one day. You must have working knowledge of any language first.
Read More..

Find all the Provinces with districts with all Municipalities in Nepal.
Read More..

The purpose of this library is to combine features of `google map` such as map, marker and corresponding info window rendering to a single API call. This simplifies implementing google map into your project while not having to remember all the associated API calls provided by it. Just include the script tag into your project and initialize and object of `ideaMap` class with appropriate parameters while rendering the map. While instantiating the `ideaMap` class, there are certain parameters that you need to pass according to your requirement. Let’s go over them, Here, we’ve passed in an object while initializing the map.
Read More..




Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.