Rails:如何使用 after_commit 方法从模型传递参数,最终在 Twitter 上分享
我正在尝试使用 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
但这是据我的理解,我在其他地方读到我可以传递“自我”而不是“标题”和“内容”,并且仍然能够使用“标题”和“内容”以及模型中的任何其他内容,例如“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: 语法错误,意外的keyword_self,期待')' def twitter_share(self)
并将其发布到 twitter #<帖子:0x00000101d6e1e0>
我的问题是如何正确设置传递“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
当我去删除帖子时,出现错误:
Started POST "/posts/ 32" 对于 127.0.0.1 在 2011-04-15 14:57:17 -0700 由 PostsController#destroy 作为 HTML 处理 参数:{“authenticity_token”=>“x8KkqLLCLdTOouUfCMzyWWmwxLIKThnE1n3rQNSkew8=”,“id”=>“32”} 用户负载 (1.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 5) LIMIT 1 在 82 毫秒内完成
ActionController::RoutingError (没有路由匹配 {:action=>"destroy", :controller=>"posts"}): app/models/post.rb:37: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
I'm trying to use an after_commit method to pass parameters from the post to the user model where it is shared to twitter with another method.
It works fine when I just pass it something from the post model like 'title' or 'content':
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
But this is as far as my understanding goes, I've read elsewhere that I could pass 'self' instead of 'title' and 'content', and still be able to use 'title' and 'content' plus any thing else from the model such as 'created_at'. However, I can't seem to get this working, I've tried this :
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
And I get SyntaxError (/Users/ihal/Desktop/dayor/app/models/user.rb:118: syntax error, unexpected keyword_self, expecting ')'
def twitter_share(self)
And it posts this to twitter #< Post:0x00000101d6e1e0>
My question is how to you properly setup passing 'self' so that any parameter could be called with twitter.update()?
Also how do you go about pulling out URL for the post, so that you could pass the URL to share on twitter?
Edit:
trying Rails.application.routes.url_helpers.post_url(@post, :host => 'myhost.com')
in 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
When I go to delete a post, I get the error :
Started POST "/posts/32" for 127.0.0.1 at 2011-04-15 14:57:17 -0700
Processing by PostsController#destroy as HTML
Parameters: {"authenticity_token"=>"x8KkqLLCLdTOouUfCMzyWWmwxLIKThnE1n3rQNSkew8=", "id"=>"32"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 5) LIMIT 1
Completed in 82ms
ActionController::RoutingError (No route matches {: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'
Rendered /Users/ihal/.rvm/gems/ruby-1.9.2-p136@rails3gemset/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)
post controller
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用观察者?这样你就可以将 after_commit 的东西(它似乎不属于模型)保留在适当的位置。另外,它有助于简化您的模型,而不是使其变得混乱。
对于语法错误,
self
是保留字。重命名方法声明中的变量名称。因此,请尝试以下操作:要访问控制器外部的 url 帮助程序,请使用:
访问控制器外部的 url 帮助程序时,不要忘记使用 :host 选项,以便帮助程序具有上下文。
Have you thought about using an Observer? That way you can keep the after_commit stuff, which does not appear to to belong in the model, in its proper place. Plus it helps simplify your model instead of cluttering it up.
For the syntax error,
self
is a reserved word. Rename the variable name in the method declaration. So try something like:To access the url helpers outside of the controller, use:
Don't forget to use the :host option when accessing the url helpers outside the controller so the helper has context.
我在代码中看到的是,标题和内容是 Post 的属性。因此将它们作为名称传递将被转换为它们的值。
现在您正在从 post 将 self 传递到 twitter_share 中,然后 post 对象将传递给该方法。您需要如下修改 twitter_share 方法才能使其工作
what I can see in code is, title and content are attributes of Post. so passing them as theire name will be converted into their value.
now you are passing self in twitter_share from post then the post object will be passed to that method. you need to modified the twitter_share method as below to make it work
您正在将 Post 传递到 user.twitter_share 方法中。
You are passing a Post into the user.twitter_share method.