이것은 일반적인 오류이며 연구를 수행하여 해결할 수 있음을 알고 있습니다. 그러나 나는 왜 내 응용 프로그램이 내 코드를 위반하는지 알 수 없습니다. 모두 질서가있는 것 같습니다. 나는 여분의 눈이 나를 이길 수 있기를 바라고있다.Ruby on Rails : NoMethodError in users # show
내가 점점 오전 오류는 NoMethodError in Users#show
undefined method
[] 무기 호에 대한 : NilClass` 사전에
감사
모델 :
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :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
보기 :
<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>
from <%= @user.get_location %>
</div>
</div>
<hr />
<div class="books">
<% @user.get_books.each do |book| %>
<p><%= book["name"] %></p>
<% end %>
</div></div>
<div class="col-lg-6">
<% @users.posts.order("created_at DESC").each do |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 you sure?'} %></span>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
컨트롤러 :
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
당신은 정말한다고 행 번호와 스택 추적을 할 수 있습니다. 프로덕션 환경에 있습니까? 그렇지 않다면, '더 나은 오류'보석을 사용해보십시오. 실제 REPL로 훨씬 멋진 오류 페이지를 제공하여 전체 상처를 줄일 수 있습니다. –
아마도 set_user 메소드는 사용자를 찾지 않습니다. 당신의 param 'id'가 맞습니까? – rhernando