【问题标题】:Referencing controller variables in bootstrap tooltip在引导工具提示中引用控制器变量
【发布时间】:2014-01-20 21:12:02
【问题描述】:

如果一条推文包含属于当前用户的blockedshow 的任何phrase,它会收到redacted div。当它悬停在推文上时,我希望工具提示告诉用户推文包含哪个特定短语。现在我正在使用@phrases,但它不起作用。有什么想法吗?

查看

<% if is_redacted? tweet %>
    <!-- Tweet contains at least one blocked phrase. -->
    <a href="https://www.twitter.com/#{tweet.user.screen_name}" 
       data-toggle="tooltip" title="This tweet contains the phrase:<%=@phrases%> "> 
        <%= check_if_redacted(tweet.text)%>
    </a>
<% end %>

控制器

  class TwitterController < ApplicationController
  def index
  end

  def login
  end

  def tweet
    text = params[:my_tweet]
     Client.update(text) unless text==nil

end

  private

  def is_redacted? tweet
    @phrases ||= current_user.blockedshows.map(&:phrases).flatten.map(&:text)
    @phrases.any? { |phrase| tweet.text.include? phrase }
  end

  helper_method :is_redacted?

end

【问题讨论】:

  • 它不起作用是什么意思? @phrasesnil?
  • 是的,只有“此推文包含短语:”出现在工具提示中。
  • 也添加控制器动作的代码。
  • 看起来您正在以两种方式检查 redacted
  • 这是 rails 还是其他框架?

标签: ruby twitter-bootstrap twitter


【解决方案1】:

实例变量@phrases 在辅助方法中定义。我认为它在视图范围内不可见。您需要改为在控制器操作中启动。如果这是在索引中:

def index
  @phrases = current_user.blockedshows.map(&:phrases).flatten.map(&:text)
end

我对你的tweet 方法有点困惑。这是一个控制器动作还是只是一个辅助方法。我看起来像一个辅助方法,因为您在视图中引用它(除非这里没有看到更多代码)。我也会将推文放在一个实例变量中,甚至可能是 is_redacted。类似于:

def index
  @phrases = current_user.blockedshows.map(&:phrases).flatten.map(&:text)
  @tweet = Client.update(params[:my_tweet]) unless text==nil
  @isredacted = @phrases.any? { |phrase| tweet.text.include? phrase }
end

然后在视图中使用:

<% if @isredacted %>
  <!-- Tweet contains at least one blocked phrase. -->
  <a href="https://www.twitter.com/#{@tweet.user.screen_name}" 
    data-toggle="tooltip" title="This tweet contains the phrase:<%=@phrases%> "> 
    <%= check_if_redacted(@tweet.text)%>
  </a>
<% end %>

【讨论】:

  • 谢谢,现在@phrases 没有出现为零。我可以在工具提示中看到所有用户的短语。但是我如何才能看到推文包含的特定短语?
  • 我对您的数据模型了解得不够多,无法回答这个问题。看起来您希望推文的文本与短语完全匹配。是这样吗?
猜你喜欢
  • 1970-01-01
  • 2019-03-17
  • 2013-05-15
  • 1970-01-01
  • 2014-01-07
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
相关资源
最近更新 更多