开发者

dynamic nested form always creates an extra blank entry - using formtastic_coocoon

I'm using formtastic & formtastic_cocoon to created a nested form.

All seems to be working well dynamically adding a nested form to an existing form, with one exception.

I have users and users have entries.

When I create a user, and add an entry, I end up with

-User
   - Entry (empty)
   - Entry Test 1

I should only have

-User
   - Entry Test 1

I'm not sure why the blank entry is always showing up.

My models are

class User < ActiveRecord::Base
   validates :name, :presence => true
   has_attached_file :photo

   has_many :tasks, :dependent => :destroy

   accepts_nested_attributes_for :tasks, :allow_destroy => true

end

class Task < ActiveRecord::Base
   attr_accessible :entry

   belongs_to :user


end

my create controller is (I think this is the right controller)

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[开发者_运维技巧:notice] = "Successfully created user."
      redirect_to @user
    else
      render :action => 'new'
    end
  end

 def create
    @task = Task.new(params[:task])
    if @task.save
      flash[:notice] = "Successfully created task."
      redirect_to @task
    else
      render :action => 'new'
    end
  end

The empty entries are showing up in the database, so I don't think it is a problem with the html.erb files, but I can post those here if that would help.


Turns out this may be an issue with the way formtastic_cocoon handles forms.

When looking at the html source, the nested form is in the page, but hidden.

I changed the model to

accepts_nested_attributes_for :tasks, :reject_if=> proc {|attributes| attributes[:entry].blank?}, :allow_destroy => true
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜