Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I haven't tried or tested it but I guess you can do something like:</p> <pre><code>def share_all if user.authentications.where(:provider =&gt; 'twitter').any? user.twitter_share(title, content, post_url(self, :host =&gt; "your_host")) end end </code></pre> <p>Prior to that, in your model add this:</p> <pre><code>include ActionController::UrlWriter </code></pre> <p>This will make the url helper available in your model as well. You can read <a href="https://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models">this</a> to get more information about it.</p> <p>Please try this as well (found it on <a href="https://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models">this page</a> again):</p> <pre><code>Rails.application.routes.url_helpers.post_url(self, :host =&gt; "your_host") </code></pre> <p>[EDIT]</p> <p>I have just read your <a href="https://gist.github.com/923619" rel="nofollow noreferrer">gist</a>, what you should do is this instead:</p> <pre><code>## 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 =&gt; "localhost:3000") if user.authentications.where(:provider =&gt; 'twitter').any? user.twitter_share(url) end end </code></pre> <p>Or:</p> <pre><code> 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 =&gt; "localhost:3000") if user.authentications.where(:provider =&gt; 'twitter').any? user.twitter_share(url) end end </code></pre> <p>It is <strong>very</strong> important that you get that url inside the <em>share_all</em> 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.</p> <p>I have tested those two variants and they work just well :).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload