I've been stuck for a while trying to add tags to posts. I'm using http://railscasts.com/episodes/258-token-fields as guide.
Post form has nested images and tags. Tags are handled as authors in Ryan's episode.
When creating new post, I'm getting, stack level too deep, error.
Any ideas?
Model
class Post < ActiveRecord::Base
attr_accessible :title, :body, :keywords, :description, :post_images_attributes, :tags
has_and_belongs_to_many :tags
has_many :post_images, :dependent => :destroy
validates :title, :presence => true
validates :body, :presence => true
validates :keywords, :presence => true
validates :description, :presence => true
extend FriendlyId
friendly_id :title, use: :slugged
accepts_nested_attributes_for :post_images, :allow_destroy => true #, :reject_if => lambda { |a| a[:image].blank? }
accepts_nested_attributes_for :tags, :allow_destroy => true
attr_reader :tags
def tags=(ids)
self.tags = ids.split(",")
end
end
Form
<%= form_for @post, :html => {:multipart => true} do |f| %>
<div class="field">
<%= f.label :title %><br/>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords %>
</div>
<div class="field">
<%= f.label :description %><br/>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :tags %><br/>
<% if !@post.tags.blank? %>
<%= f.text_field :tags, "data-pre" => @post.tags.map(&:attributes).to_json %>
<% else %>
<%= f.text_field :tags %>
<% end %>
</div>
<%= f.fields_for :post_images do |builder| %>
<%= render "post_image_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Image", f, :post_images %></p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Log
Started POST "/posts" for 127.0.0.1 at 2012-03-29 15:43:57 +0200
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"r/oWrpb4AOAwMrp+G6vOUmaQPFh32
NsabUa2h+O5KrY=", "post"=>{"title"=>"Test post", "body"=>"Test body", "keywords"
=>"key, words", "description"=>"description", "tags"=>"1", "post_images_attribut
es"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x392e410 @original_fi
lename="africa-web-l.jpg", @content_type="image/jpeg", @headers="Content-Disposi
tion: form-data; name=\"post[post_images_attributes][0][image]\"; filename=\"afr
ica-web-l.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/user
/AppData/Local/Temp/RackMultipart20120329-5772-dl0ix3>>, "is_main"=>"1", "_destr
oy"=>"false"}}}, "commit"=>"Create Post"}
Completed 500 Internal Server Error in 24ms
SystemStackError (stack level too deep):
Stack level too deep means that you are recursively changing an attribute. It happens here:
def tags=(ids)
self.tags = ids.split(",")
end
You need to use different name for this method.