Rails:如何传递 url 参数?

发布于 2024-11-01 23:30:56 字数 454 浏览 0 评论 0原文

我需要将每个帖子的 url 传递到用户模型中,以便可以将其共享到 Twitter。现在我可以传递帖子的属性,例如标题和内容,这些属性会共享到 Twitter,但我似乎不知道如何传递帖子网址。提前致谢。

帖子.rb

after_commit :share_all

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

用户.rb

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-11-08 23:30:56

我还没有尝试或测试过它,但我想你可以这样做:

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

在此之前,在你的模型中添加以下内容:

include ActionController::UrlWriter

这将使 url 帮助器在你的模型中也可用。您可以阅读来获取有关它的更多信息。

请也尝试一下(在这个上找到它再次页面):

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

[编辑]

我刚刚阅读了您的要点,您应该做什么是这个:

## 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

或者:

  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

share_all 方法内部而不是外部获取该 url 非常重要,因为无论是内部还是外部, self 的值都不一样。当它位于方法内部时, self 引用调用 share_all 方法的 Post 实例。当它在外部时,它是类 Post 本身。

我已经测试了这两个变体,它们工作得很好:)。

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 :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文