How do I update & display a current user's tag

2019-08-17 04:15发布

I'm not sure how to display tags in my view that belong to a user logged in with Omniauth.

A page in the view loads a random photos and tags associated to it (via a form that can be updated from that page). It works, but when I log in with Facebook account A, it will show exactly the same tags as if I log in with Facebook account B.

Getting the whiny nil error with this below Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

The view is:

<%= render 'tag_form' %>

Updated: The form is:

<%= 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.

The tags are also called in a user's dashboard, as below (currently empty because obviously I can't update tags right now):

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

The application controller showing current user

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

The Brand controller

helper_method :current_user 

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

Added: The Brand model

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条回答
一夜七次
2楼-- · 2019-08-17 04:47

Was just talking to you on IRC, but can you try:

<%= debug @brand.tag_list %>

in your view. Your user doesn't have tags. If you want tags for your user add a acts_as_taggable in your User model


It seems you want to find tags of @brand tagged by current_user. In that case:

<%= debug @brand.taggings(:tagger_id => current_user.id, :tagger_type => 'User').all %> 
查看更多
The star\"
3楼-- · 2019-08-17 05:07

I removed this from my model:

before_save :set_tag_owner

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

I added this to the controller in an action that handles PUT requests instead of GET requests (Update method)

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

It works now.

查看更多
登录 后发表回答