Twitter 集成 Rails 4 应用程序 [英] Twitter integration rails 4 app

查看:58
本文介绍了Twitter 集成 Rails 4 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这个 gem 将 twitter 提要集成到我的 Rails 4 应用程序中.>

我已阅读 github 页面上的文档,但在尝试使用我认为的方法时遇到了问题.

我在名为 twitter_init.rb 的初始化文件中有以下内容(带有我的身份验证信息):

client = Twitter::Streaming::Client.new do |config|config.consumer_key = "YOUR_CONSUMER_KEY"config.consumer_secret = "YOUR_CONSUMER_SECRET"config.access_token = "YOUR_ACCESS_TOKEN"config.access_token_secret = "YOUR_ACCESS_SECRET"结尾

当我尝试在我的视图中使用以下代码时,我收到错误 undefined local variable or method 'client' for class#<#<class...>:...>:

<% client.sample do |tweet|%><%= tweet.text %><%结束%>

是否还有其他步骤可以公开客户端变量,或者我没有正确初始化它?

解决方案

当您在启动文件中将 client 设置为 Twitter::Streaming::Client.new 时,您创建了一个局部变量.这个变量失去了作用域,并且在您进入视图时早已消失.

要让它工作,请删除 twitter_init.rb

然后在正在执行的控制器动作中

@client = Twitter::Streaming::Client.new do |config|config.consumer_key = "YOUR_CONSUMER_KEY"config.consumer_secret = "YOUR_CONSUMER_SECRET"config.access_token = "YOUR_ACCESS_TOKEN"config.access_token_secret = "YOUR_ACCESS_SECRET"结尾

然后在视图中

<% @client.sample do |tweet|%><%= tweet.text %><%结束%>

注意@符号.

然而,这确实违反了瘦控制器厚模型约定,应该在其他地方初始化,比如在服务中.

I'm attempting to integrate a twitter feed into my Rails 4 application using this gem.

I've read the documentation on the github page but I'm running into a problem when trying to use a method in my view.

I have the following in an initializer file named twitter_init.rb (with my auth info):

client = Twitter::Streaming::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"
end

When trying to use the following code in my view I receive the error undefined local variable or method 'client' for class#<#<class...>:...>:

<% client.sample do |tweet| %>
   <%= tweet.text %>
 <% end %>

Is there another step to exposing the client variable or maybe I didn't initialize it correctly?

解决方案

When you set client to the Twitter::Streaming::Client.new in you initiate file you created a local variable. This variable lost scope and is long gone by the time you get to your view.

To get it working remove twitter_init.rb

Then in the controller action that is being executed

@client = Twitter::Streaming::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"
end

Then in the view

<% @client.sample do |tweet| %>
  <%= tweet.text %>
<% end %>

Notice the @ symbols.

This does however violate the skinny controller thick model convention and should be initialized elsewhere like in a service.

这篇关于Twitter 集成 Rails 4 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆