Ruby on Rails: NoMethodError in Users#show -
i know common error , can solved doing research (which have done) however, cannot seem figure out why app breaking when code seems in order. i'm hoping eyes can me past this.
the error getting nomethoderror in users#show undefined method[] nil:nilclass`
thanks in advance
model:
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable has_many :posts, dependent: :destroy def self.find_or_create_from_auth_hash(auth_hash) find_by_auth_hash(auth_hash) || create_from_auth_hash(auth_hash) end def self.find_by_auth_hash(auth_hash) where( provider: auth_hash.provider, uid: auth_hash.uid ).first end def self.create_from_auth_hash(auth_hash) create( provider: auth_hash.provider, uid: auth_hash.uid, email: auth_hash.info.email, name: auth_hash.info.name, oauth_token: auth_hash.credentials.token, oauth_expires_at: time.at(auth_hash.credentials.expires_at) ) end def password_required? super && provider.blank? end def update_with_password(params, *options) if encrypted_password.blank? update_attributes(params, *options) else super end end def facebook @facebook ||= koala::facebook::api.new(oauth_token) end def get_profile_info self.facebook.get_object("me") end def get_location h = get_profile_info["location"] h ["name"] end def get_books self.facebook.get_connection("me", "books") end def get_profile_picture self.facebook.get_picture(uid) end end view:
<div class="container"> <div class="row"> <div class="col-lg-4"> <div class="media"> <%= image_tag @user.get_profile_picture, class: "pull-left" %> <div class="media-body"> <h4 class="media-heading"><%= @user.name %></h4> <%= @user.get_location %> </div> </div> <hr /> <div class="books"> <% @user.get_books.each |book| %> <p><%= book["name"] %></p> <% end %> </div></div> <div class="col-lg-6"> <% @users.posts.order("created_at desc").each |post| %> <div class="post"> <h5><%= post.title %></h5> <p><%= post.content %></p> <span class="pull-right label label-info"><%= time_ago_in_words(post.created_at) %> ago</span> <% if post.user == current_user %> <span class="pull-left"><%= link_to 'edit', edit_post_path(post), class: "btn btn-xs btn-primary" %> <%= link_to 'destroy', post, class: "btn btn-xs btn-danger", method: :delete, data: {confirm: 'are sure?'} %></span> <% end %> </div> <% end %> </div> </div> </div> controller:
class userscontroller < applicationcontroller before_action :set_user, only: [:show] def index @users = user.all end def show end private def set_user @user = user.find(params[:id]) end end
i spy 3 candidates.
two in model
def get_location h = get_profile_info["location"] # <-- here h ["name"] # <-- here (most likely) end one in view
<% @user.get_books.each |book| %> <p><%= book["name"] %></p> <!-- <-- here --> <% end %> the accessed facebook data might nil in 1 of places.
Comments
Post a Comment