1

I am very new to rails and I am trying to get an image url using Graph API inside a partial view using a controller method. How can I return the image url stored in an instance variable from the controller method to the place where I called it.

For example: I have a partial "_fb_comments.erb" and I am trying to call the controller method "fb_comments" in my "SocialMediaController" from "_fb_comments.erb" partial like this ->

<img src=<%= social_media_fb_comments_path(:comm_usr_id => "#{comm['from'['id']}" %> />

And I have my "fb_comments" controller method inside "SocialMediaController" defined like this ->

def fb_comments
user=User.find_by_id(session[:uid])
@comm_user=params[:comm_usr_id]
@get_url=Koala::Facebook::API.new(user.token)
@image_url=@get_url.get_picture(@comm_user)
render :text => @image_url

There is no view associated with this controller method. Tell me if I am missing something and how can i get this @image_url value to my <img src= /> tag in my partial view. Please help me with the code as I am completely new to rails.

1 Answer 1

2

You don't need a controller action, you need a view helper. You can put the method in application_controller and add the line helper_method :fb_comments' at the top ofapplication_controller`.

class ApplicationController << ActionController::Base
  helper_method :fb_comments

  def fb_comments(options)
    user=User.find_by_id(session[:uid])
    comm_user=options[:comm_usr_id]
    get_url=Koala::Facebook::API.new(user.token)
    get_url.get_picture(comm_user)
  end

Then call it with...

<img src=<%= fb_comments(comm_usr_id: "#{comm['from'['id']}" %> />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.