Miguel Angel Quintana
Miguel Angel Quintana

Reputation: 1399

Current_user && User profile

So in my To-do app I have profiles for each user that displays a list of all the Items they have created like so :

<% if current_user %>
<h1>Hello, <%= current_user.username.capitalize%>, </h1>
<% else %>
<h1><%= @user.username.capitalize%> To - Do List. </h1>
<%end %>

<h4> List of Items </h4>
<%= render :partial => 'items/form', :locals =>{:item => Item.new} %>
<% @user.items.order('created_at DESC').each do |item| %>
<%= render :partial => 'items/item' , :locals => {:item => item } %>
<% end %>

My problem is that I want users to be able view other Users profile but with the Header changing according to who is viewing it.

<h1>Hello, <%= current_user.username.capitalize%>, Here is your To-do Items</h1>

if its the current_users own profile or:

<h1><%= @user.username.capitalize%> To - Do List. </h1>

if the user is viewing another users profile.

As you can see I tried a little something with an if statement but it doesn't seem to be working as i want it to .Can anyone help me out please?

Upvotes: 2

Views: 469

Answers (3)

Uday kumar das
Uday kumar das

Reputation: 1613

use this code:

 current_user == @user if condition.

Upvotes: 2

Ben Hawker
Ben Hawker

Reputation: 949

In your controller you may have:

@user = User.find(params[:id])

Then in your view something along the line of

<% if current_user == @user %>

Upvotes: 2

Dharam Gollapudi
Dharam Gollapudi

Reputation: 6438

Try current_user == @user in if condition.

If there is a possibility that current_user could be nil, then

Try current_user.present? && current_user == @user in if condition.

Upvotes: 3

Related Questions