Why am I getting error Undefined method `name' for nil:NilClass with Ruby on Rails?

倖福魔咒の 提交于 2020-01-04 21:35:54

问题


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.


回答1:


Quick things you should be aware of the rails framework and the ruby language:

  • A function defined in any of your helpers will be available to all helpers and views (so there is no reason to copy and paste the same functions through different helpers);
  • You're probably using an authentication gem and I guess it is the Devise gem. If this is right, then you should not be overriding their helpers unless you have a reason to do this;
  • User.anything will 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_attribute will 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:

user = current_user.try(:remember_token)

EDITED: The question is a bit messy, but I also think the following code will work with your controller:

def profile
  @user = User.find_by_remember_token(params[:remember_token])
end

You must access the request's parameters through the params hash.




回答2:


EDIT: completely replaces my first answer with one hopefully not as stupid :-)

(While there are several ways to implement and manage sessions in Rails, the default uses a cookie in the browser to reference a key stored in memory. Sessions are created by a request from a browser, so while it's certainly possible to use the console to get at an existing session, it's probably not what you want.)

So your method, user_from_remember_token will either return a user or nil. What I don't see in your code is where you're setting the remember_token on the User model. I'll assume it's there, but you may want to have code that tells the user to log in if you don't find them. A common pattern would be

def current_user
  @current_user ||= user_from remember_token
  unless @current_user
    flash[:notice] = "Yo!  Log in first."
    redirect_to login_path and return
  end
end

There's no problem calling a model finder from a separate controller. But why call User.find_by_remember_token(:remember_token) -- you don't have the remember_token yet (right?). Don't you just want to call the current_user method in your sessions helper?

If the method is not visible, you may want to include or require the session helper in your application_controller.rb



来源:https://stackoverflow.com/questions/9811419/why-am-i-getting-error-undefined-method-name-for-nilnilclass-with-ruby-on-rai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!