link_to(body, url, html_options = {})
link_to(body, url_options = {}, html_options = {})
link_to(options = {}, html_options = {}) do
end
link_to(url, html_options = {}) do
end
Because it relies on url_for, link_to supports both older-style controller/action/id arguments and newer RESTful routes. Current Rails style favors RESTful routes whenever possible, so base your application on resources and uselink_to "Profile", profile_path(@profile)
html <a href="/profiles/1">Profile</a>
or the even pithier
link_to "Profile", @profile
html <a href="/profiles/1">Profile</a>
in place of the older more verbose, non-resource-oriented
link_to "Profile", :controller => "profiles", :action => "show", :id => @profile
html <a href="/profiles/show/1">Profile</a>
Similarly,
link_to "Profiles", profiles_path
html <a href="/profiles">Profiles</a>
is better than
link_to "Profiles", :controller => "profiles"
html <a href="/profiles">Profiles</a>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
<%= link_to(@profile) do %>
<strong><%= @profile.name %></strong> -- <span>Check it out!</span>
<% end %>
<strong>David</strong> -- <span>Check it out!</span></a>```
Classes and ids for CSS are easy to produce:
link_to "Articles", articles_path, :id => "news", :class => "article"<br>
```html <a href="/articles" class="article" id="news">Articles</a>```
<h1>link_to_if</h1>
```<%= link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) %>```<br>
If the user isn't logged in...<br>
=> ```<a href="/sessions/new/">Login</a>```
<h1>link_to_unless</h1>
```<%= link_to_unless(@current_user.nil?, "Reply", { :action => "reply" }) %>```<br>
If the user is logged in...<br>
```=> <a href="/controller/reply/">Reply</a>```
<h1>link_to_unless_current</h1>
```html
<ul id="navbar">
<li><%= link_to_unless_current("Home", { :action => "index" }) %></li>
<li><%= link_to_unless_current("About Us", { :action => "about" }) %></li>
</ul>
- Adjusting URLs through abstraction.
- ...?
- Profit!
Assume we have a controller for dealing with articles app/controllers/posts_controller.rb:
def new
@post = Post.new
end
def edit
@post = Post.find(params[:id])
end
The corresponding view app/views/posts/new.html.erb using form_for looks like this:
<%= form_for @post do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= f.submit "Create" %>
<% end %>
The resulting HTML is:
<form accept-charset="UTF-8" action="/posts/create" method="post">
<input id="post_title" name="post[title]" size="30" type="text" />
<textarea id="post_body" name="post[body]" cols="60" rows="12"></textarea>
<input name="commit" type="submit" value="Create" />
</form>
Keep in mind that this: ```ruby <%= form_for @post do |f| %> ... <% end %> ``` is equivalent to something like:
<%= form_for @post, :as => :post, :url => posts_path, :html => { :class => "new_post", :id => "new_post" } do |f| %>
...
<% end %>
and if @post is an existing record you want to edit: ```ruby <%= form_for @post do |f| %> ... <% end %> ``` this is equivalent to something like: ```ruby <%= form_for @post, :as => :post, :url => post_path(@post), :method => :put, :html => { :class => "edit_post", :id => "edit_post_45" } do |f| %> ... <% end %> ```
Syntax: image_tag(source, options = {}) public
Returns an html image tag for the source. The source can be a full path or a file that exists in your public images directory.
#####Examples:
image_tag("icon") # =>
<img src="/images/icon" alt="Icon" />
image_tag("icon.png") # =>
<img src="/images/icon.png" alt="Icon" />
image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # =>
<img src="/images/icon.png" width="16" height="10" alt="Edit Entry" />
image_tag("/icons/icon.gif", :size => "16x16") # =>
<img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
image_tag("/icons/icon.gif", :height => '32', :width => '32') # =>
<img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
image_tag("/icons/icon.gif", :class => "menu_icon") # =>
<img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # =>
<img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # =>
<img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
The image assets are to be kept in /app/assets/images
image_tag('foo') vs. <img src="/images/foo.png" />
image_tag("rails.png")
# => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />
<img src="/images/foo.png?63565665" />
to
<img src="http://assets.yourcompany.com/images/foo.png?63565665" />