Currently I have a header that links the user to his profile through this:
<li><%= link_to '<i class="icon-picture"></i> My Profile'.html_safe, current_user %></li>
I would like to be able to link the current user to his profile by doing something like this:
<li>
<a href="<%= current_user %>">
<div><%= image_tag current_user.avatar (:small) %></div>
<strong>Surge Pedroza</strong><br/>
view my profile page
</a>
</li>
But I do not know how to do it through an <a href="...">. Can someone please help me out?
This would be a better way to accomplish what you want:
<%= link_to current_user do %>
<div><%= image_tag current_user.avatar(:small) %></div>
<strong><%= current_user.full_name %></strong><br/>
view my profile page
<% end %>
link_to .. do is useful for the exact situation you are describing. Check the documentation for more examples.
I actually got it :)
<a href="<%= current_user.id %>">
Related
Ok, I'm gonna try to get as specific as I can. Here's my code:
routes.rb
root to: "users#index"
match "/users/:id/about", to: "users#about", as: :about
match "/users/:id/blog", to: "users#blog", as: :blog
match "users/:id/contact", to: "users#contact", as: :user_contact
match "/contact", to: "static_pages#contact", as: :static_contact
resources :sessions, only: [:new, :create, :destroy]
resources :users do
resources :portfolios do
resources :pictures
end
end
Here's my navigation bar with all the links on my main page:
<nav>
<ul class="sf-menu" id="nav">
<li class="<%= "selected" if current_page?(root_path) %>"><%= link_to "home", root_path %></li>
<li class="<%= "selected" if current_page?(about_path) %>"><%= link_to "about me", about_path %></li>
<li><%= link_to "my portfolio" %>
<ul>
<% #user.portfolios.each do |portfolio| %>
<li><%= link_to portfolio.name, user_portfolio_path(#user, portfolio) %></li>
<% end %>
</ul>
</li>
<li class="<%= "selected" if current_page?(blog_path) %>"><%= link_to "blog", blog_path %></li>
<li class="<%= "selected" if current_page?(user_contact_path) %>"><%= link_to "contact", user_contact_path %></li>
</ul>
</nav>
When I link from any link (home, about me, blog, contact) to any other link everything works fine. For example when I'm on the about me page (/users/8/about) I can link to the contact page(users/8/contact). The problem is when I land on the portfolio page (/users/8/portfolios/31) and try to link to the about me page, for example, the url becomes this: /users/31/about instead of back to /users/8/about. User 31 doesn't exist but portfolio 31 exists.
I'm trying to use one of the icons in Bootstrap for a link_to.
These don't work::
<% link_to '<i class="icon-pencil"></i>', task_path(task)%>
<% link_to task_path(task), html_options = {:class => 'icon-pencil'} %>
I would appreciate the help!
You can use this other form of the link_to helper:
<%= link_to task_path(task) do %>
<i class="icon-pencil"></i>
<% end %>
Make sure to include the = sign before the link_to call.
I have a partial that contains a header, a subheader and potentially one or more buttons. It is used on many different pages. Often the pages don't need buttons, so I just pass in the header and an optional subheader to the partial. However sometimes the pages need one or more buttons and the only way I've managed to allow for an arbitrary number of buttons to be passed in is using content_for. My partial looks like this:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= content_for :page_header_buttons %>
</ul>
</header>
<% end %>
This use of content_for nasty. Is there any way I can pass the list items / buttons into this partial? How else could I deal with this situation?
You could transform this partial into a layout:
<% if defined? page_title %>
<header class="pageHeader">
<div class="page-details">
<h3><%= page_title %></h3>
<% if defined? page_subtitle %>
<p><%= page_subtitle %></p>
<% end %>
</div>
<ul class="crud-menu nav-pills">
<%= yield %>
</ul>
</header>
<% end %>
And you would render it like that:
<%= render layout: "your_partial_above", locals: { page_title: "page title } do %>
<%= render "partial_with_your_buttons" %>
<% end %>
I'm trying to create an index page where all assets are displayed including a link to the user who they are assigned to, however when I use
def index
#assets = Asset.paginate(page: params[:page])
#user = User.find(params[:id])
end
It returns - couldn't find User without an ID.
index.html.erb - Assets
<%= provide(:title, 'Assets') %>
<h1>All assets</h1>
<%= will_paginate #assets %>
<ol class="assets">
<%= render #assets %>
</ol>
<%= will_paginate #assets %>
_asset.html.erb partial
<li>
<span class="id">JTC<%= asset.id %></span>
<span class="serial"><%= asset.serial_no %></span>
<span class="status">Status: <%= asset.status %></span>
<span class="type">Type: <%= asset.asset_type %><br/></span>
<span class="description">Description: <%= asset.asset_description %><br /></span>
<span class="comment"><em><%= asset.comment %></em></span>
<span class="timestamp">
<br />Added <%= time_ago_in_words(asset.created_at) %> ago. <br /> Assigned to: <%= link_to
#user.name %>
<br />Date Purchased: <%= asset.date_purchased %>
</span>
</li>
#user = User.find(params[:id]) is expecting a URL along the lines of /assets/40, where 40 is the id (and even then that wouldn't make sense, since the 40 should be the id of the asset, not the user). When you just access /assets, there is no id parameter, so it tries to basically do User.find(nil), which gives you that error.
Assuming assets belong to users, you should just get rid of the #user = User.find(params[:id]) line in your controller, and change your partial to use asset.user.name instead of #user.name.
To avoid an N+1 query situation on your users, you could update your controller to use this:
#assets = Asset.includes(:user).paginate(page: params[:page])
Anyone else working through Chapter 11 Exercises for Michael Hartl's Rails Tutorial 2nd Edition?
Chapter 11, Exercise 3 asks:
Refactor Listing 11.31 by adding partials for the code common to the following/followers pages, the Home page, and the user show page.
I'm not seeing anything worth refactoring in the homepage, user show page, or the show_follow page
If anyone came up with something worthwhile for this exercise, would love to know.
Thanks!
You can refactor the first block of code from Listing 11.31:
<section>
<%= gravatar_for #user %>
<h1><%= #user.name %></h1>
<span><%= link_to "view my profile", #user %></span>
<span><b>Microposts:</b> <%= #user.microposts.count %></span>
</section>
because it is essentially the same as the views\shared_user_info.html.erb partial used on the home page (Listing 10.32). Therefore, you can replace the block of code above with this:
<%= render 'shared/user_info' %>
Note that you will also need to add <% #user ||= current_user %> to the top of the views\shared_user_info.html.erb partial (which is the same as what was necessary to add to the stats partial in Listing 11.20).
Additionally, there is some duplication (though not exact duplication) between the feed_item + feed partials with the user + micropost partials, where depending on which page is being displayed (follow_show, home, or profile) there are one or more elements being listed (name, gravatar, admin delete link, micropost content, micropost time stamp, and micropost delete link). Those could probably be refactored too to eliminate the feed_item+feed partials and replace them with a combination of the user + micropost partials depending on the page.
I just worked through this exercise and found a solution that works.
First I changed around app/views/shared/_user_info.html.erb to use the #user variable if it is set, and the current_user variable otherwise.
app/views/shared/_user_info.html.erb:
<% if #user %>
<%= link_to gravatar_for(#user, size: 52), #user %>
<h1>
<%= #user.name %>
</h1>
<span>
<%= link_to "view my profile", #user %>
</span>
<span>
<%= pluralize(#user.microposts.count, "micropost") %>
</span>
<% else %>
<%= link_to gravatar_for(current_user, size: 52), current_user %>
<h1>
<%= current_user.name %>
</h1>
<span>
<%= link_to "view my profile", current_user %>
</span>
<span>
<%= pluralize(current_user.microposts.count, "micropost") %>
</span>
<% end %>
Then I replaced the corresponding information in app/views/users/show_follow.hmtl.erb with the partial _user_info.html.erb
app/views/users/show_follow.hmtl.erb:
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/stats' %>
<% if #users.any? %>
<div class="user_avatars">
<% #users.each do |user| %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="span8">
<h3><%= #title %></h3>
<% if #users.any? %>
<ul class="users">
<%= render #users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
I hope this answer helps anyone going through M. Hartl's tutorial.