ruby on rails - undefined method `tokens' for "2":String -
i'm new rails , programming , keep getting above error when try view user id "2". i'm using twitter-omniauth , twitter gems view users tweets. have no clue whats wrong, appreciated.
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
these models.
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
and in application helper
module applicationhelper def user_has_provider?(provider, user) @user.token.by_name(provider).any? end end
error:
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'
issue on line: feed = feed.new(params[:id])
def feed @title = "feed" @providers = providers.for(@user) @user = user.find(params[:id]) feed = feed.new(params[:id]) # here @ 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
it should be:
def feed @title = "feed" @providers = providers.for(@user) @user = user.find(params[:id]) feed = feed.new(@user) # should @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
because, feed
's constructor expects user object , you're passing params[:id]
"2" , hence error: undefined method `tokens' “2”:string.
Comments
Post a Comment