0

I need to pass the url for each post into user model so it can be shared to twitter. Right now I can pass attributes of the post, such as title and content, which is shared to twitter, but I can't seem to figure out how to pass the post url. Thanks in advance.

post.rb

after_commit :share_all

def share_all
 if user.authentications.where(:provider => 'twitter').any?
  user.twitter_share(self)
 end
end

user.rb

def twitter_share(post) 
 twitter.update("#{post.title}, #{post.content}")           #<--- this goes to twitter feed 
end
trying_hal9000
  • 4,113
  • 8
  • 41
  • 62

1 Answers1

1

I haven't tried or tested it but I guess you can do something like:

def share_all
 if user.authentications.where(:provider => 'twitter').any?
  user.twitter_share(title, content, post_url(self, :host => "your_host"))
 end
end

Prior to that, in your model add this:

include ActionController::UrlWriter

This will make the url helper available in your model as well. You can read this to get more information about it.

Please try this as well (found it on this page again):

Rails.application.routes.url_helpers.post_url(self, :host => "your_host")

[EDIT]

I have just read your gist, what you should do is this instead:

## posts.rb
  after_commit :share_all
  def share_all
     # note that I am using self inside the method not outside it.
     url =  Rails.application.routes.url_helpers.post_url(self, :host => "localhost:3000")
     if user.authentications.where(:provider => 'twitter').any?
        user.twitter_share(url)  
    end
  end

Or:

  include ActionController::UrlWriter #very important if you use post_url(..) directly
  after_commit :share_all
  def share_all
     # if you use the url helper directly you need to include ActionController::UrlWriter
     url =  post_url(self, :host => "localhost:3000")
     if user.authentications.where(:provider => 'twitter').any?
        user.twitter_share(url)  
    end
  end

It is very important that you get that url inside the share_all method and not outside it, because self has not the same value whether it's inside or outside. When it's inside the method, self references the instance of Post on which the share_all method is called. When it's outside it's the class Post itself.

I have tested those two variants and they work just well :).

Community
  • 1
  • 1
Amokrane Chentir
  • 28,497
  • 36
  • 111
  • 156
  • I get an undefined method post_url for # – trying_hal9000 Apr 16 '11 at 21:30
  • Ahh sorry for this. Of course it's undefined, the url helper is only available for the views. See my edit that should be ok now. – Amokrane Chentir Apr 16 '11 at 21:41
  • Thanks for the help, it says I need a :host set so I tried using this Rails.application.routes.url_helpers.posts_url(:host => "example.com") but I get a routing error no route matches action destroy posts controller – trying_hal9000 Apr 16 '11 at 21:59
  • Ok, test done :). This "WILL" work: Rails.application.routes.url_helpers.post_url(self, :host => "localhost:3000"). Don't put the 's' at the end of post. (so it's post_url not postS_url). – Amokrane Chentir Apr 16 '11 at 22:59
  • I'm getting undefined method for post_url again, I have it just like you say, here's my code https://gist.github.com/923619 – trying_hal9000 Apr 16 '11 at 23:35
  • Ok, take a look at my latest edit! You are doing two separate mistakes. 1/ You are using self outside the scope of the method (line 6). 2/ You are calling the helper without including ActionController:UrlWriter) line 12. Choose either one of the two variants that I have written in my edit. – Amokrane Chentir Apr 16 '11 at 23:55
  • Thank you so much for explaining that to me! I've got it working now:) You're the man! – trying_hal9000 Apr 17 '11 at 04:31