我如何更新&显示当前用户的标签,标记为acts_as_taggable_on?

时间:2011-08-03 21:40:51

标签: ruby-on-rails ruby-on-rails-3 tagging omniauth acts-as-taggable-on

我不确定如何在我的视图中显示属于使用Omniauth登录的用户的标记。

视图中的页面会加载与其关联的随机照片和标签(通过可从该页面更新的表单)。它有效,但是当我使用Facebook帐户A登录时,它将显示完全相同的标签,就像我使用Facebook帐户B登录一样。

通过以下方式获取whiny nil错误为nil调用id,这将错误地为4 - 如果你真的想要id为nil,请使用object_id

观点是:

<%= render 'tag_form' %>

已更新:表单为:

<%= form_for @brand, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :tag_list, "Your tags" %> <%= f.text_field :tag_list, :value => @brand.all_tags_list %>
</p>
<p><%= f.submit "Tag" %></p>
<% end %>

# <%= f.text_field :tag_list %> was changed to the one above.

标签也会在用户的信息中心中调用,如下所示(目前为空,因为显然我现在无法更新标签):

<%= brand.taggings( :tagger_id => current_user.id, :tagger_type => 'User').collect{|tagging| tagging.tag.to_s}.join(", ") %>

显示当前用户的应用程序控制器

def current_user  
  @current_user ||= User.find(session[:user_id]) if session[:user_id]  
end

品牌控制器

helper_method :current_user 

def index
  @brands = Brand.all
  @brand = Brand.order("RANDOM()").first
end

已添加:品牌型号

attr_accessible :name, :tag_list, :current_user
acts_as_taggable_on :tags

belongs_to :user

before_save :set_tag_owner

def set_tag_owner
  set_owner_tag_list_on(@brand, :tags, self.tag_list)
  self.tag_list = nil
end

2 个答案:

答案 0 :(得分:0)

刚刚在IRC上与您交谈,但您可以尝试:

&lt;%= debug @ brand.tag_list%&gt;

在你看来

。您的用户没有标签。如果您想为用户添加标签,请在用户模型中添加acts_as_taggable


您似乎想要查找由current_user标记的@brand标签。在那种情况下:

<%= debug @brand.taggings(:tagger_id => current_user.id, :tagger_type => 'User').all %> 

答案 1 :(得分:0)

我从我的模型中删除了这个:

before_save :set_tag_owner

def set_tag_owner
    set_owner_tag_list_on(@clown, :tags, self.tag_list)
    self.tag_list = nil
end

我在处理PUT请求而不是GET请求的操作中将此添加到控制器中(更新方法)

current_user.tag(@clown, :with => params[:brand][:tag_list], :on => :tags)

现在有效。

相关问题