【问题标题】:How to call a text_field based on tag?如何根据标签调用 text_field?
【发布时间】:2015-04-07 18:14:48
【问题描述】:

我不知道这是否可能,但如果用户在评估表中创建一个名为任务声明的标签:

<%= f.text_field :name %>
<%= f.text_field :tag_list, valuation: @valuation.tag_list.to_s.titleize %>

我们怎样才能让那个估值的:name出现在首页:jumbotron

<% content_for :jumbotron do %>
      <h1>Mission</h1>
      <p>
        # Mission Statement Goes Here
        <% @valuation_mission.each do |valuation_mission| %>
          <%= valuation_mission.name %>
        <% end %>
      </p>
<% end %>

我假设我们必须像我尝试的那样在 pages_controller 中编写一个方法:

class PagesController < ApplicationController
  def home
      @user = current_user
    if logged_in?
      @habits = current_user.habits
      @valuations = current_user.valuations
      @accomplished_goals = current_user.goals.accomplished
      @unaccomplished_goals = current_user.goals.unaccomplished
      @averaged_quantifieds = current_user.quantifieds.averaged
      @instance_quantifieds = current_user.quantifieds.instance
      @valuation_mission = current_user.valuations #We'd need to add .something to make this work?
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
   end
end

我正在使用acts-as-taggable-on gem,我从这里学习了如何实现它:http://railscasts.com/episodes/382-tagging

感谢您的宝贵时间!

【问题讨论】:

  • current_user.valuations 应该返回与current_user 关联的所有(可以为空!)估值记录的列表
  • 好吧酷我修改了问题@MrYoshiji
  • 您究竟想对用户的估值做什么?您只需执行current_user.valuations 即可获得它们。例如,如果您想获取所有名称之间的逗号,请使用current_user.valuations.map(&amp;:name).join(', ')
  • 我试图只用标签:任务声明来拉估值@MrYoshiji。
  • current_user.valuations 返回什么?一个字符串数组?如果是,那么current_user.valuations.select{ |tag| tag.match(/mission-statement/) }

标签: ruby-on-rails ruby model-view-controller tags acts-as-taggable-on


【解决方案1】:

你可以用这个:

current_user.valuations.tagged_with('mission-statement')

在文档中找到:https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多