我的应用未正确加载当前登录用户或查看其他用户的帖子。我是rails的新手,并且暂时出现了这个错误。如果需要其他任何东西,请告诉我,我会得到它。
这是错误跟踪
Started GET "/feed_content" for 127.0.0.1 at 2014-09-11 10:53:14 -0700
Processing by UsersController#feed as */*
User Load (2.0ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL
LIMIT 1
Completed 500 Internal Server Error in 9ms
NoMethodError (undefined method `tokens' for nil:NilClass):
app/helpers/application_helper.rb:14:in `user_has_provider?'
app/models/feed.rb:26:in `twitter_posts'
app/models/feed.rb:17:in `posts'
app/controllers/users_controller.rb:70:in `feed'
user_controller.rb
class UsersController < ApplicationController
def feed
@title = "Feed"
@user = User.find_by(id: params[:id])
feed = Feed.new(@user)
@providers = Providers.for(@user)
@timeline = feed.posts(params[:twitter_pagination]) # line 70
@unauthed_accounts = feed.unauthed_accounts
@poster_recipient_profile_hash = feed.poster_recipient_profile_hash
@commenter_profile_hash = feed.commenter_profile_hash
@load_more_url = feed_content_path(
:twitter_pagination => feed.twitter_pagination_id,
)
render 'show_feed'
end
feed.rb
class Feed
include ApplicationHelper
attr_reader :poster_recipient_profile_hash,
:commenter_profile_hash,
:unauthed_accounts,
:twitter_pagination_id,
def initialize(user)
@user = user
@unauthed_accounts = []
end
def posts(twitter_pagination_id)
TimelineConcatenator.new.merge(twitter_posts(twitter_pagination_id))
end
private
def twitter_posts(twitter_pagination_id)
twitter_posts = []
if user_has_provider?('twitter', @user)
twitter_timeline = Twitter::Timeline.new(@user)
begin
twitter_posts = twitter_timeline.posts(twitter_pagination_id).map { |post| Twitter::Post.from(post) }
@twitter_pagination_id = twitter_timeline.last_post_id
rescue Twitter::Error::Forbidden, Twitter::Error::Unauthorized
@unauthed_accounts << "twitter"
end
twitter_posts
else
twitter_posts
end
end
apphelper.rb
module ApplicationHelper
def user_has_provider?(provider, user = current_user)
@user.tokens.by_name(provider).any?
end
end
token.rb
class Token < ActiveRecord::Base
validates :provider, presence: true
validates :uid, presence: true
belongs_to :user
def self.by_name(name)
where(provider: name)
end
def self.update_or_create_with_twitter_omniauth(id, auth)
token = where(provider: auth["provider"], uid: auth["uid"]).first_or_initialize
token.provider = auth["provider"]
token.uid = auth["uid"]
token.access_token = auth["extra"]["access_token"].token
token.access_token_secret = auth["extra"]["access_token"].secret
token.user_id = id
token.save!
token
end
def configure_twitter(access_token, access_token_secret)
client = Twitter::REST::Client.new do |config|
config.consumer_key = '**********'
config.consumer_secret = '********'
config.access_token = access_token
config.access_token_secret = access_token_secret
end
client
end
end
feed_index.js
FeedIndex = {
initialize: function () {
var reloadOk = false;
$.get("/feed_content").success(function (response) {
var loadingMessage = $(".loading_message");
loadingMessage.before(response);
loadingMessage.hide();
}).success(function () {
reloadOk = true
});
$(document).scroll(function () {
var scrollbarPosition = $(this).scrollTop();
var documentHeight = $(this).height();
if (documentHeight - scrollbarPosition < 7500 && reloadOk === true) {
$(".loading_message").show();
var nextPageUrl = $(".load_posts_link a").attr("href");
reloadOk = false;
$.get(nextPageUrl).success(function (response) {
reloadOk = true;
$(".load_posts_link").replaceWith(response);
$(".loading_message").hide();
});
}
});
EDIT *****
的routes.rb
resources :users do
member do
get :following, :followers, :bio, :feed, :settings
end
end
get '/feed_content', to: 'users#feed', as: :feed_content
答案 0 :(得分:1)
def user_has_provider?(provider, user = current_user)
@user.tokens.by_name(provider).any?
end
我会使用params传递的用户(不带@):user.tokens.by_name(provider).any?
答案 1 :(得分:0)
问题是您没有定义@user
变量 - 因此它显示为nil
我认为这是因为你使用的是collection
路线(用于多条记录),而不是member
路线(用于个别记录):
#config/routes.rb
resources :users do
get :feed_content, action: :feed, as: :feed_content #-> domain.com/users/:user_id/feed_count
end
这将使您能够调用以下内容:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def feed
...
@user = User.find params[:user_id]
...
end
end
此应该允许您填充@user
变量,然后您就可以将该用户发送给您的帮助者。目前,您的助手正在尝试使用@user
,而实际上它只需要使用传递给它的数据:
#app/helpers/application_helper.rb
class ApplicationHelper
def user_has_provider?(provider, user = current_user)
user.tokens.by_name(provider).any?
end
end