Forum: Ruby on Rails help needed regaring Acts As Taggable On Steroids

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0a1b70165620ceca363224b2024cd999?d=identicon&s=25 Mac Eros (mac_eros)
on 2008-12-11 15:59
Hello,
      In my rails application, I have a book model.
      I want to add tags. For  that I have installed
      acts_as_taggable_on_steroids  from this link
     http://agilewebdevelopment.com/plugins/acts_as_tag...
     and do as the readme file.
 This is my Model :
  class Book < ActiveRecord::Base
    acts_as_taggable
  end

 Here is my ApplicationHelper :
module ApplicationHelper
  include TagsHelper
end

And I couldn't get the idea what should I add in the controller to save
tags(please guide me to save tags) . I just try to follow the readme and
Edited part of BookController is:

def create
    @book = Book.new(params[:book])
    @book.tag_list # []
    @book.tag_list = "Funny, Silly"
   respond_to do |format|
      if @book.save
        flash[:notice] = 'Book was successfully created.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created,
:location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status =>
:unprocessable_entity }
      end
    end
  end

def tag_cloud
      @tags = Books.tag_counts
    end

In the new.html.erb I have added these lines :
<% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
    <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class
=> css_class %>
  <% end %>

After I restarting the server, When I want to create a new book entry it
shows error as below:

"You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?"
And the error details shows that the error is in tag_cloud

When I add a TagList.delimiter = " "  in config/environment.rb
and restart the server.. the server stop with error showing as
uninitialized constant TagList .

Please help me as I am new in ruby.
Thanks in advance.
87303c28b23af9aa69831b6bd156fdb0?d=identicon&s=25 Ruhul Amin (tuhin_cse99)
on 2008-12-12 02:34
Mac Eros wrote:
> Hello,
> Edited part of BookController is:
>
> def create
>     @book = Book.new(params[:book])
>     @book.tag_list # []
>     @book.tag_list = "Funny, Silly"
>    respond_to do |format|
>       if @book.save
>         flash[:notice] = 'Book was successfully created.'
>         format.html { redirect_to(@book) }
>         format.xml  { render :xml => @book, :status => :created,
> :location => @book }
>       else
>         format.html { render :action => "new" }
>         format.xml  { render :xml => @book.errors, :status =>
> :unprocessable_entity }
>       end
>     end
>   end
>
> def tag_cloud
>       @tags = Books.tag_counts
>     end
>
> In the new.html.erb I have added these lines :
> <% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
>     <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class
> => css_class %>
>   <% end %>
>

I have done almost the same but in create methos I have place the code
@book = Book.new(params[:book])
    @book.tag_with(params[:tag_list])
   respond_to do |format|
    ......
and in new.html.erb
<%= text_field_tag 'tag_list', @book.tags.collect{|t| t.name}.join(" ")
%>

But when I run the application and try to create a new Entry
 if shows the error that "undefined method `tag_with' for
#<Book:0x4a8bc14>
"
Please help me.
Thanks in advance.
This topic is locked and can not be replied to.