I thought methods such as name and email were default in rails?
In my static pages view, in profile.html.erb I have:
<% if logged_in? %>
<% provide(:title, @user.name) %>
<% else %>
<% provide(:title, 'Profile')%>
<% end %>
I put in my static_page_controller
def profile
@user = User.find_by_remember_token(:remember_token)
end
When I go to the console User.find_by_remember_token(“actualtoken”).name returns me the appropriate users name, but :remember_token does not. How do I make :remember_token = the logged in users remember token?
In my sessions_helper I have
def log_in(user)
cookies.permanent[:remember_token] = user.remember_token
current_user = user
end
def logged_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
def log_out
current_user = nil
cookies.delete(:remember_token)
end
private
def user_from_remember_token
remember_token = cookies[:remember_token]
User.find_by_remember_token(remember_token) unless remember_token.nil?
end
end
copying it to my static_pages_helper didn’t accomplish anything.
Quick things you should be aware of the rails framework and the ruby language:
User.anythingwill call the static function anything from the User class;user = User.find_by_anything(the_thing)is a class static helper provided by ActiveModel that will query the database looking for a user that has *anything = the_thing*; this user or nil will be returned;user.an_attributewill call a function that returns the user specified attribute (which is the same as the column name of this attribute by default);user.try(:anything)will try to call the function anything from the user and return its value. If user is nil, the returned value will also be nil.That said, I guess you just wanted to retrieve the current user remember token, which can be accomplished with the following:
EDITED: The question is a bit messy, but I also think the following code will work with your controller:
You must access the request’s parameters through the params hash.