how to print user_id using the devise gem - ruby-on-rails

I currently trying to do something like this in the applicaiton.html.erb
<h1> <% print "Customer id #{current_user}"%> </h1>
but I get nothing. the 'Customer id' is not being printed either. What am I doing wrong ?
Thanks for your time

If you're in a Rails view, using erb, then print or puts are not needed.
The <%= %> allows you to render (print) the expression that's contained inside, while <% %> allows you to create Ruby logic without rendering it, the same way <%# %> works for comments, so you could do:
<h1>
<%= "Customer id #{current_user.id} "%>
</h1>
Or:
<h1>Customer id <%= current_user.id %></h1>
No need to concatenate, and in both cases accessing to the current_user id attribute, otherwise it'd return the whole User object.

Related

How to access specific object fields?

I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>

Ruby on Rails partial not rendering properly

I have an Opportunity model and a User model. On my Opportunity index page, I have a table that lists all of the Users. When I click "Show" next to Users, the page that renders is a blank page, whereas it should say "test test test". Here is my code:
views/opportunities/index.html.erb:
<td><%= link_to 'Show', user %></td>
views/users/show.html.erb:
<% render #user %>
views/users/_user.html.erb:
test test test test
Please help!! Thanks!!
<% render #user %> will not display any result, use <%= render #user %> to display the result of the evaluated code.
Executes the ruby code within the brackets.
<% %>
Prints something into erb file.
<%= %>
Avoids line break after expression.
<% -%>
Comments out code within brackets; not sent to client (as opposed to HTML comments).
<%# %>

Strings not displayed right

So, I have this which displays emails to a user.
OLD CODE FOR REFERENCE:
<%= for email in #emails
# print the name
eml = email
eml
puts "<br>"
end
%>
FIXED, WORKING, STABLE CODE:
<% for email in #emails %>
<%= email %>
<br>
<% end %>
<%= puts #emails.inspect %>
As you can see, it was a problem of multiple line tag. Bazar that It would cause this problem, but not at all that it would cause A problem.
OLD:
And it is working great. One thing. So, EML is a ruby string with HIDDEN#HIDDEN.HIDDEN, but when it goes to display I get this on the rendered page: ["HIDDEN#HIDDEN.HIDDEN"], so why is it doing that? Inspected it, it isn't a hash. Just a string. What is happening here?
This syntax does not look quite right. If this is being rendered in a view using ERB, you probably want code that looks more like this:
<% #emails.each do |email| %>
<%= email %><br />
<% end %>
The way you have that written is very C#-looking. In Ruby it is more common to use the methods attached to the object. Enumerable objects like arrays could be iterated through using the each method and a special structure in Ruby called a block.
http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-each

Ruby How to display message if include? statement is true or false

I'm very new to Ruby and am attempting to display one of two messages based on whether certain words are included in an array. My current controller contains the following:
#cookie = ["gluten", "sugar", "dairy", "chocolate"]
And my view contains this:
<%= #cookie.include?"gluten" %>
The above returns 'true' and prints on the page just fine. However, nothing gets printed on the page when trying either of the following methods. The page renders fine with no errors, but no messages:
<%= puts "Sorry" if #cookie.include?"gluten" %>
and
<%=
if #cookie.include?("gluten")
puts "Sorry, you cannot eat this."
else
puts "You have the greenlight."
end
%>
I'm hoping that I'm creating a very simple mistake in syntax or am misunderstanding the usage of the include? function.
looking at the ruby it appears to be sound, http://rubyfiddle.com/riddles/d6798.
Must be rails error so try
<% if #cookie.include?("gluten") %>
<%= "Sorry, you cannot eat this." %>
<% else %>
<%= "You have the greenlight." %>
<% end %>
If you want it all in one erb statement, you would do something like this:
Don't use "puts". The "<%=" is already doing a 'to_s' and will output the value to the response body. You may have seen the results show up in your logs if you used 'puts'.
Also - for erb, the <% ... %> syntax, that is for logic, often an 'each' or 'if' state, the <%= %> is for writing to the screen. Most of the time it is the attribute of a ruby object like <%= user.name %>

Rails - Easy way to display all fields in view

OK I'm sure I'm missing something here, but please forgive me I'm new to Rails.
Is there some way in Rails to display all the fields for an object rather than specifying each?
In my show.html template rather than going
<p>Name: <%=h #user.full_name %></p>
<p>Email: <%=h #user.email %></p>
I just want a oneliner to do this without having to type out each of the 15 or so fields I have.
Its an admin page so its fine if all the fields are shown (id, created_at, etc.)
If this was PHP it would take me about 5 secs using foreach, but I've googled (on the wrong things obviously) for an hour with no luck.
Thanks!
Something like
<% for attribute in #user.attributes.keys %>
<p><%= attribute.humanize %> <%= #user.attributes[attribute].to_s %></p>
<% end %>
could do the trick.
Matt
I suppose you want to display all attributes of a row from database table which is defined as ActiveRecord model. You can use class method column_names (every ActiveRecord model has it), which returns names of table columns in an array.
<%= User.column_names.collect { |col_name| "#{col_name.capitalize}: <p>#{#user[col_name]}</p>" }.join("\n") %>
<%= debug #user %>
simple way to show the object... that is what I usually use anyway!
#user.attributes.each{|key, value| puts "#{key} : #{value}"}
This is the snippet I used to blacklist some attributes I didn't want to show...
controller (user_controller.rb)
def show
keys_blacklist = %W(user_id name) #these are the fields to hide
#user_showlist = #user.attributes.except(*keys_blacklist)
end
view (show.html.erb):
<!-- language: ruby --><% for attribute in #user_showlist.keys %>
<b><%= attribute.humanize %></b>
<%= #user.attributes[attribute].to_s %>
<!-- language: ruby --><% end %>
You can also use instead:
#user_showlist = #user.attributes.slice(*keys_whitelist)
in order to display a whilelist of properties.
If you are using haml and want to loop through the attributes on for example a user object in a view:
- for attribute in #user.attributes.keys
%p
= attribute.humanize
= #user.attributes[attribute].to_s

Resources