Closed vincentcordel closed 9 years ago
Hey Vincent,
How are you calling @short_url in the view?
I assumed I would just call @url.shor_url but this doesn't work
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.
Hey Phil,
Thanks for helping me out. Here's what I have.
gem 'bitly', '~> 0.10.4'
Bitly.use_api_version_3
Bitly.configure do |config|
config.api_version = 3
config.access_token = "0a9e6c23bb42efd17feffa3266dbd4aa7ec8d045"
end
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)
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!
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.
Good catch, that was it!
Thank you so much for solving this, the day starts right :)
Excellent! Hope the rest of the app goes well!
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
My initializer uses oAuth:
Now when I'm trying to get in the view the short_url, I have in my controller show
Now this is where it gets confusing.
Why is that? Where is my mistake?
Thanks!