【问题标题】:Rails: How to pass a post's URL in an after_commit methodRails:如何在 after_commit 方法中传递帖子的 URL
【发布时间】:2011-04-16 02:10:52
【问题描述】:

我在弄清楚如何为每个用户帖子(在我的应用中称为 supportposts)创建一个 url 并将其传递给 after_commit 方法时遇到了一些真正的麻烦。现在我可以传递支持帖子的属性,例如标题和内容,这些属性将共享到 twitter:

supportpost.rb

after_commit :share_all

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

用户.rb

def twitter_share(title, content) 
  twitter.update("#{title}, #{content}")           #<--- this goes to twitter feed 
end

现在我真正想做的是将 supportpost 的 URL 分享到 twitter 提要中。我正在尝试像这样访问模型外部的 url 助手,但它弄乱了我的路线,我得到了 RoutingError (No route matches {:action=>"destroy", :controller=>"supportposts"})

supportpost.rb

after_commit :share_all

Rails.application.routes.url_helpers.supportpost_url(@supportpost, :host => 'examplehost.com') 

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

我在这里做错了什么?如何正确地将 URL 传递到 twitter_share?

这是我的路线和后备控制器/模型http://pastie.org/1799492

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 url parameter-passing


    【解决方案1】:
    def share_all
      if user.authentications.where(:provider => 'twitter').any?
        supportpost_url = Rails.application.routes.url_helpers.url_for :action => :show, :controller => 'supportposts', :id => self, :host => 'example.com'
        user.twitter_share(supportpost_url)
      end
    end
    

    Rails.application.routes.url_helpers.supportpost_url(@supportpost, :host => 'examplehost.com') 你拥有它不会有任何好处。哦,顺便说一句,我在这个例子中使用了url_for(),但你也可以像在上面的问题中那样使用supportpost_url()。这里的主要内容是您调用该方法的位置。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 2013-10-15
      • 2022-01-23
      相关资源
      最近更新 更多