Hidden view-helper methods in Rails
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>"
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>
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>
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>
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
Shiva Bhusal (Software Engineer) 2 Min. Read Oct 1, 2019
rails ruby views helpers
Read More..
Learn Dart Language In One Day
Shiva Bhusal (Software Engineer) 2 Min. Read Sep 4, 2019
Build Mob App Android iOS Dart Flutter Introduction
Read More..
List Of All Municipalities By Districts By Provinces Of Nepal
Shiva Bhusal (Software Engineer) 22 Min. Read Sep 2, 2019
Data Ruby Hash Nepal Districts Municipalities Province
Read More..
Easily Integrate Maps In Your Projects
Shiva Bhusal (Software Engineer) 4 Min. Read Aug 12, 2019
JS Javascript Google Map Open Street Map NPM Nodejs Plugins
Read More..
Read More..
Ruby On Rails Interview Practice Questions And Answers
Shiva Bhusal (Software Engineer) 0 Min. Read Oct 15, 2018
Ruby Rails Interview Questions Practice
Read More..