【问题标题】:Rails: How to pass parameters from a model using an after_commit method, eventually to share on twitterRails:如何使用 after_commit 方法从模型中传递参数,最终在 twitter 上分享
【发布时间】:2011-08-06 15:25:52
【问题描述】:

我正在尝试使用 after_commit 方法将参数从帖子传递到用户模型,然后通过另一种方法将其共享到 twitter。

当我从帖子模型中传递一些东西,比如“标题”或“内容”时,它工作得很好:

post.rb

after_commit :share_all

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

user.rb

def twitter_share(title, content) 
  twitter.update("#{title}, #{content}")
end

但就我的理解而言,我在其他地方读到过我可以传递“self”而不是“title”和“content”,并且仍然可以使用“title”和“content”以及任何模型中的其他内容,例如“created_at”。但是,我似乎无法正常工作,我已经尝试过:

post.rb

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

user.rb

def twitter_share(self) 
  twitter.update("#{title}, #{content}")
end

我得到 SyntaxError (/Users/ihal/Desktop/dayor/app/models/user.rb:118: syntax error, unexpected keyword_self, expecting ')' def twitter_share(self)

并将其发布到 twitter #

我的问题是如何正确设置传递 'self' 以便可以使用 twitter.update() 调用任何参数?

另外,您如何提取帖子的 URL,以便您可以传递 URL 以在 twitter 上分享?

编辑:

尝试 Rails.application.routes.url_helpers.post_url(@post, :host => 'myhost.com')

在 post.rb 中

 class Post < ActiveRecord::Base  # line 19

 after_commit :share_all

 Rails.application.routes.url_helpers.post_url(@post, :host => 'myhost.com')    #line 37

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

当我去删除一个帖子时,我得到了错误:

在 2011-04-15 14:57:17 -0700 为 127.0.0.1 开始 POST "/posts/32" PostsController#destroy 处理为 HTML 参数:{"authenticity_token"=>"x8KkqLLCLdTOouUfCMzyWWmwxLIKThnE1n3rQNSkew8=", "id"=>"32"} 用户负载 (1.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 5) LIMIT 1 82ms 完成

ActionController::RoutingError (没有路由匹配 {:action=>"destroy", :controller=>"posts"}): app/models/post.rb:37:in <class:Post>' app/models/post.rb:19:in' app/controllers/posts_controller.rb:36:in `authorized_user'

在救援/布局中渲染 /Users/ihal/.rvm/gems/ruby-1.9.2-p136@rails3gemset/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb ( 1.2ms)

后控制器

def destroy
  @post.destroy
  redirect_to root_path
end


private
   def authorized_user
   @post = Post.find(params[:id])   #line 36
     redirect_to root_path unless current_user?(@spost.user)
   end
end

【问题讨论】:

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


    【解决方案1】:

    您是否考虑过使用Observer?这样,您可以将似乎不属于模型的 after_commit 内容保留在适当的位置。此外,它还有助于简化您的模型,而不是把它弄得杂乱无章。

    对于语法错误,self 是保留字。重命名方法声明中的变量名。所以试试类似的东西:

    def twitter_share(post)
      # do stuff
    end
    

    要访问控制器外部的 url 助手,请使用:

    Rails.application.routes.url_helpers.post_url(@post, :host => 'myhost.com') 
    

    在访问控制器外部的 url 帮助器时不要忘记使用 :host 选项,以便帮助器具有上下文。

    【讨论】:

    • 您好,感谢您的帮助。我试图让上面的代码正常工作,但遇到了一些问题,即当我添加 Rails.application.routes 时......我的销毁操作失败并说没有路由。我在上面进行了编辑以显示发生了什么。
    • Rails.application.routes.url_helpers.post_url(@post, :host =&gt; 'myhost.com' 将@post 指定的帖子的网址作为字符串返回。那应该在您需要 url 的方法中。我的猜测是在 twitter_share 里面。
    • 嗯,我仍然收到错误 No route matches {:action=>"destroy", :controller=>"posts"} 使用 Rails.applications.routes.url_helpers 代码时,有什么想法吗?
    • routes.rb 是什么样的?确保它包含读取resources :posts 的行,或者您手动指定了删除帖子的路线。如果您进行任何更改,您将不得不重新启动 rails 服务器。
    • 我的 routes.rb 中有资源 :supportposts, :only => [:create, :destroy, :index, :show] 当我删除 Rails.applications 时,删除帖子的所有内容都可以正常工作.routes.url.. 代码,当我运行 rake 路由时,我有这条路由 post DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"} 我很困惑
    【解决方案2】:

    我在代码中看到的是,title 和 content 是 Post 的属性。因此将它们作为它们的名称传递将被转换为它们的值。

    现在你在 twitter_share 中从 post 传递 self 然后 post 对象将被传递给该方法。您需要修改 twitter_share 方法如下使其工作

    def twitter_share(post)
     twitter.update("#{post.title}, #{post.content}")
    end
    

    【讨论】:

      【解决方案3】:
      def twitter_share(post) 
        twitter.update("#{post.title}, #{post.content}")
      end
      

      您正在将 Post 传递给 user.twitter_share 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-20
        • 1970-01-01
        • 2011-03-31
        • 1970-01-01
        • 2020-01-15
        • 2017-08-30
        • 2020-12-27
        • 1970-01-01
        相关资源
        最近更新 更多