1

I have the following code:

<div class="category_name">
     <%= current_user %>
     <%= review.user.front_name %>
      <% if current_user.name = review.user.front_name %>
      <%= link_to "You", user_path(review.user) %> / <%= review.categories_list %>
      <% else %>
      <%= link_to review.user.front_name, user_path(review.user) %> / <%= review.categories_list %>
      <% end %>
    </div>

It renders something like:

Tom Murphy Bill Smith You / Films

Why is this happening?

<% if current_user.name = review.user.front_name %>

In other words, if Tom Murphy = Bill Smith...which it doesn't...shouldn't it go to the line AFTER the 'else'? That's what I want to happen.

1
  • 6
    its a silly mistake, used =instead of == . You are assigning instead of comparing Commented Jun 18, 2013 at 10:24

4 Answers 4

5

replace <% if current_user.name = review.user.front_name %> with

<% if current_user.name == review.user.front_name %>

you put =, you have to put ==

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for all your help. They're all right, but just accepting this as it came in first. Isn't there also a '==='? What does that do?
regarding '===' The general definition of the triple equals is that it will return true if both parts are identical or if the right part is contained within the range of the left. 1 === 1 #true (1..3) === 2 #true There are some other impacts as well
2

Just modify: <% if current_user.name = review.user.front_name %> to

<% if current_user.name == review.user.front_name %>

The == ( double equal) Test for equal value..

Comments

2

In Ruby to check equality you should use double equal signs (==).

Also - Currently you are comparing the names. You should compare the objects (User) to avoid mistakes or even worse (security breaches).

<% if current_user == review.user %>

Comments

1

when you want to make conditional statement. you should compare your statement. for example:

if number == 10
   print "Variable is 10"
elsif number == "20"
   print "Variable is 20"
else
   print "Variable is something else"
end

on your case please change the line:

<% if current_user.name = review.user.front_name %>

change intoenter code here

<% if current_user.name == review.user.front_name %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.