Created
December 29, 2022 13:44
-
-
Save DevGW/2a6eb079489ff5cae4254a79ff562568 to your computer and use it in GitHub Desktop.
Ruby / Rails :: Using link_to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simplest form: | |
<%= link_to "Home", root_path %> | |
# => <a href="/">Home</a> | |
# Linking to a resource: | |
<%= link_to "Profile", user_path(@user) %> | |
# => <a href="/users/1">Profile</a> | |
# Using link_to with a block | |
<%= link_to root_path do %> | |
<%= content_tag :span, "Home", :class => "home-link" %> | |
<% end %> | |
# => <a href="/"><span class="home-link">Home</span></a> | |
# Adding html classes and/or id to the link: | |
<%= link_to "Section", root_path, :class => "my-class", :id => "my-id" %> | |
# => <a href="/" class="my-class" id="my-id">Section</a> | |
# How to delete a record with link_to: | |
<%= link_to "Remove", @user, :method => :delete %> | |
# => <a rel="nofollow" data-method="delete" href="/users/1">Remove</a> | |
# Require confirmation for deleting a record: | |
<%= link_to "Remove", @user, :method => :delete, :data => {:confirm => "You Sure?"} %> | |
# => <a data-confirm="You Sure?" rel="nofollow" data-method="delete" href="/users/1">Remove</a> | |
## IMAGES | |
# Linking to an image with link_to: | |
<%= link_to image_tag('logo.png'), root_path %> | |
# => <a href="/"><img src="/assets/logo-c88948e05e11587af2c23747862ca433.png" alt="Logo"></a> | |
# or pass the image in a block: | |
<%= link_to root_path do %> | |
<%= image_tag('logo.png') %> | |
<% end %> | |
# => <a href="/"><img src="/assets/logo-c88948e05e11587af2c23747862ca433.png" alt="Logo"></a> | |
# Add an alt attribute to the image: | |
<%= link_to image_tag('logo.png'), root_path, :alt => "MyLogo" %> | |
# => <a href="/"><img src="/assets/logo-c88948e05e11587af2c23747862ca433.png" alt="MyLogo"></a> | |
# Linking to an image path: | |
<%= link_to "Logo", image_path('logo.png') %> | |
# => <a href="/assets/logo-c88948e05e11587af2c23747862ca433.png">Logo</a> | |
# Anchors: | |
<%= link_to "Section", root_path(:anchor => "interesting-section") %> | |
# => <a href="/#interesting-section">Section</a> | |
# Ajax links with link_to remote: | |
<%= link_to "Ajax", root_path, :remote => true %> | |
# => <a data-remote="true" href="/">Ajax</a> | |
# POST-ing using link_to: | |
<%= link_to "Post", root_path, :method => :post %> | |
# => <a rel="nofollow" data-method="post" href="/">Post</a> | |
# You need the jquery-rails gem for this to work. | |
# Adding more params to the POST request: | |
<%= link_to "Create User", users_path(:email => "jdoe@email.com", :password => "secret"), :method => :post %> | |
# => <a rel="nofollow" data-method="post" href="/users?email=jdoe%40email.com&password=secret">Create User</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment