Ahoy, So i''ve installed the acts_as_taggable module and everything is fine, but i''m a bit confused about this bit of code described in the API "photo = Photo.new # splits and adds to the tags collection photo.tag "wine beer alcohol" # don''t need to split since it''s an array, but replaces the tags collection # trailing and leading spaces are properly removed photo.tag [ ''wine '', '' vodka''], :clear => true photo.tag_names # => [ ''wine'', ''vodka'' ] # appends new tags with a different separator # the ''wine'' tag won?t be duplicated photo.tag_names << ''wine, beer, alcohol'', :separator => '','' # The difference between +tag_names+ and +tags+ is that +tag_names+ # holds an array of String objects, mapped from +tags+, while +tags+ # holds the actual +has_and_belongs_to_many+ collection, and so, is # composed of +Tag+ objects. photo.tag_names.size # => 4 photo.tags.size # => 4 " How do i actually rig this up to my _form.rhtml so that I can add tags to an item? The above code only seems to give static tags such as "wine beer alchohol" -- Posted via http://www.ruby-forum.com/.
Will Jessup wrote:> Ahoy, > > So i''ve installed the acts_as_taggable module and everything is fine, > but i''m a bit confused about this bit of code described in the API > > "photo = Photo.new > > # splits and adds to the tags collection > photo.tag "wine beer alcohol" > > # don''t need to split since it''s an array, but replaces the tags > collection > # trailing and leading spaces are properly removed > photo.tag [ ''wine '', '' vodka''], :clear => true > > photo.tag_names # => [ ''wine'', ''vodka'' ] > > # appends new tags with a different separator > # the ''wine'' tag won?t be duplicated > photo.tag_names << ''wine, beer, alcohol'', :separator => '','' > > # The difference between +tag_names+ and +tags+ is that +tag_names+ > # holds an array of String objects, mapped from +tags+, while +tags+ > # holds the actual +has_and_belongs_to_many+ collection, and so, is > # composed of +Tag+ objects. > photo.tag_names.size # => 4 > photo.tags.size # => 4 > " > > How do i actually rig this up to my _form.rhtml so that I can add tags > to an item? The above code only seems to give static tags such as "wine > beer alchohol"I haven''t used acts_as_taggable, but one way to do it should be to create a text field (perhaps done with text_field_tag rather than text_field) called "tags", then you''d handle the new tags in your create and/or controller: @item.tag params[:tags] unless params[:tags].blank? This would add the new tags to your model @item (obviously replace this with your proper model name) if the tags field wasn''t empty. There may be a simpler way, but this is what looks good to me. Jeff Coleman -- Posted via http://www.ruby-forum.com/.
Thanks again Jeff, you''re the man.
I had actually figured it out and came to post my findings ^_^
In my actions NEW and EDIT _form.rhtml I added a field for tags
<p><label for="item_tags">Tags for this
Item</label><br />
<%= text_field_tag( ''tags'', '''',
"value" => "#{h @item.tag_names.join("
")}") %></p>
I ran into some stringify_keys! issues, which is why it''s done this
way.
And my items_controller was updated with an additional line to save the
tags
def update
@item = Item.find(params[:id])
@item.tag(params[:tags])
if @item.update_attributes(params[:item])
flash[:notice] = ''Item was successfully updated.''
redirect_to :action => ''show'', :id => @item
else
render :action => ''edit''
end
end
Perhaps I should ad this to the wiki...
--
Posted via http://www.ruby-forum.com/.
Will Jessup wrote:> Thanks again Jeff, you''re the man. > > I had actually figured it out and came to post my findings ^_^ > > In my actions NEW and EDIT _form.rhtml I added a field for tags > > <p><label for="item_tags">Tags for this Item</label><br /> > <%= text_field_tag( ''tags'', '''', "value" => "#{h @item.tag_names.join(" > ")}") %></p> > > I ran into some stringify_keys! issues, which is why it''s done this way. > > And my items_controller was updated with an additional line to save the > tags > > def update > @item = Item.find(params[:id]) > @item.tag(params[:tags]) > if @item.update_attributes(params[:item]) > flash[:notice] = ''Item was successfully updated.'' > redirect_to :action => ''show'', :id => @item > else > render :action => ''edit'' > end > end > > Perhaps I should ad this to the wiki...Nice work! One comment-- text_field_tag(name, value = nil, options = {}) The second parameter sets the starting value of the text field tag. Is that what you''re doing with your third param? text_field_tag ''tags'', h(@item.tag_names.join(" > ")) might be a more direct way of doing it. Or does that have something to do with the stringify_keys issue you mentioned? Jeff Coleman -- Posted via http://www.ruby-forum.com/.
I did it that way because in the documentation, the 2nd variable does
something like
text_field_tag(one,two) # =>
<input yadayada value="#{one.two}" />
But i need to also attach the .join(" ") and it wasn''t
working out, so I
just set the value w/ the options parameter.
Will try your method and report back.
>
> Nice work!
>
> One comment--
>
> text_field_tag(name, value = nil, options = {})
>
> The second parameter sets the starting value of the text field tag. Is
> that what you''re doing with your third param?
>
> text_field_tag ''tags'', h(@item.tag_names.join(" >
"))
>
> might be a more direct way of doing it. Or does that have something to
> do with the stringify_keys issue you mentioned?
>
> Jeff Coleman
--
Posted via http://www.ruby-forum.com/.
<%= text_field_tag ''tags'', h(@item.tag_names.join("
")) %>
works great!
Why did you have a .join(" > ") instead of .join(" ") ?
Thanks
--
Posted via http://www.ruby-forum.com/.
Will Jessup wrote:> <%= text_field_tag ''tags'', h(@item.tag_names.join(" ")) %> > > works great! > > Why did you have a .join(" > ") instead of .join(" ") ? > > ThanksMust have been just an artifact of copying quoted text. I think the join came at the end of the line, and the > appeared at the start of the quoted line. I thought that was odd at the time but didn''t look closely enough at it to take it out. :) Glad it worked for you! Jeff Coleman -- Posted via http://www.ruby-forum.com/.