philnash / bitly

🗜 A Ruby wrapper for the bit.ly API
https://rubygems.org/gems/bitly
MIT License
451 stars 139 forks source link

Can't retrieve the short url in the view?! #64

Closed vincentcordel closed 9 years ago

vincentcordel commented 9 years ago

Hello there,

I've been scratching my head for the last 2 hours and I can't find what I'm doing wrong.

Rails 4.2

gem 'bitly', '~> 0.10.4'

My initializer uses oAuth:

Bitly.use_api_version_3

Bitly.configure do |config|
  config.api_version = 3
  config.access_token = "0a9e6c23bb42efd17feffa3266dbd4aa7ec8d045"
end

Now when I'm trying to get in the view the short_url, I have in my controller show

@url = Bitly.client.shorten('http://www.google.com')
@short_url = @url.short_url

Now this is where it gets confusing.

Why is that? Where is my mistake?

Thanks!

philnash commented 9 years ago

Hey Vincent,

How are you calling @short_url in the view?

vincentcordel commented 9 years ago

I assumed I would just call @url.shor_url but this doesn't work

philnash commented 9 years ago

Can you flesh the code out a bit for me, please? Could I see what your controller action and your view does, please? Otherwise it's a bit hard to tell what's going on. Thanks.

vincentcordel commented 9 years ago

Hey Phil,

Thanks for helping me out. Here's what I have.

gemfile

gem 'bitly', '~> 0.10.4'

Initializer bitly.rb

Bitly.use_api_version_3

Bitly.configure do |config|
  config.api_version = 3
  config.access_token = "0a9e6c23bb42efd17feffa3266dbd4aa7ec8d045"
end

Stories Controller

For testing purposes, I simply added this in my controller

class StoriesController < ApplicationController
  load_and_authorize_resource
  before_action :authenticate_user!, except: [:toc, :show, :comments]

  layout "backend/index"

def show
    render :layout => "frontend/index"
    @story = Story.find(params[:id])
    @comment = Comment.new

   # Bitly add
    @bitly = Bitly.client.shorten('http://www.liberation.fr')
    @short_url = @bitly.short_url

  end
end

When I call the show story action page, the billy url gets added to the billy.com backend (I can see the link and the billy short link)

screen shot 2015-02-17 at 10 03 37 am

View

I then called the @short_url variable in my Stories show view with:

<p class='bitly-short-link'><%= @short_url %></p>

This is where I'm stuck. It doesn't show anything. So what am I missing?!

Thanks again for your help!

philnash commented 9 years ago

Hey Vincent,

I think the problem is that you're rendering the view before you set your instance variables. That might not show up when you're getting things from the database, but making an external API call is going to take a bit of time and the @short_url instance variable won't be set by the time the view gets rendered.

Try moving the line render :layout => "frontend/index" to the bottom of your action and see what happens.

vincentcordel commented 9 years ago

Good catch, that was it!

Thank you so much for solving this, the day starts right :)

philnash commented 9 years ago

Excellent! Hope the rest of the app goes well!