未定义的方法`令牌' for" 2":String

时间:2014-08-18 21:45:58

标签: ruby-on-rails omniauth twitter-oauth

我是rails和编程的新手,当我尝试查看ID为“2”的用户时,我不断收到上述错误。我正在使用twitter-omniauth和twitter gem来查看用户的推文。我不知道什么是错的,任何帮助都会非常感激。

class UsersController < ApplicationController

def feed
 @title = "Feed"
 @providers = Providers.for(@user)
 @user = User.find(params[:id])
 feed = Feed.new(params[:id])
 @timeline = feed.posts(params[:twitter_pagination])
 @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

def indexed
  @providers = Providers.for(@user)

end

这些是我的模特。

user.rb

class User < ActiveRecord::Base

has_one :token, dependent: :destroy

def validate_tokens!
  tokens.each(&:validate_token!)
end    

feed.rb

class Feed

include ApplicationHelper

def initialize(user)
  @user = user
  @unauthed_accounts = []
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

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

在我的应用程序助手

module ApplicationHelper

def user_has_provider?(provider, user)
  @user.token.by_name(provider).any?
end  
end

错误:

app/helpers/application_helper.rb:14:in `user_has_provider?'
app/models/feed.rb:27:in `twitter_posts'
app/models/feed.rb:18:in `posts'
app/controllers/users_controller.rb:62:in `feed'

1 个答案:

答案 0 :(得分:0)

问题在线:feed = Feed.new(params[:id])

def feed
  @title = "Feed"
  @providers = Providers.for(@user)
  @user = User.find(params[:id])
  feed = Feed.new(params[:id]) # HERE AT THIS LINE!!
  @timeline = feed.posts(params[:twitter_pagination])
  @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

应该是:

def feed
  @title = "Feed"
  @providers = Providers.for(@user)
  @user = User.find(params[:id])
  feed = Feed.new(@user) # Should be @user!!
  @timeline = feed.posts(params[:twitter_pagination])
  @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的构造函数需要一个用户对象,而你传递params[:id]为“2”,因此错误:未定义的方法`tokens'为“2”:String。< / p>