ruby on rails - Add extra result to Association Collection Proxy result -


i have 2 models,

class user < activerecord::base   has_many :posts end  class post < activerecord::base   belongs_to: user end 

i'm using formtastic gem, consider in edit action of users_controller. required user , associated posts attributes of form prefilled formtastic form

code:

<%= semantic_form_for @user |f| %>    <%= f.input :name %>    <%= f.inputs :posts |p| %>     <%= p.input :title %>     <%= p.input :comment %>   <% end %>  <% end %> 

for instance i'm having @user , 2 posts associated. while doing @user.posts, result like.

 [   [0] #<post:0x0000000aa53a20> {                    :id => 3,                                :title => 'hello world',               :comment => 'long text comes here'   },   [1] #<post:0x0000000aa53a41> {                    :id => 5,                                :title => 'hello world 2',               :comment => 'long text comes here too'   } ]  

so form contain 2 posts field edit.

actually, want blank post form before 2 posts.

this can easy achieved inserting new empty post object @object.posts result @ 0th position.

so, @object.posts result want should like,

    [       [0] #<post:0x0000000aa53a50> {                        :id => nil,                                    :title => nil,                   :comment => nil       },       [1] #<post:0x0000000aa53a20> {                        :id => 3,                                    :title => 'hello world',                   :comment => 'long text comes here'       },       [2] #<post:0x0000000aa53a41> {                        :id => 5,                                    :title => 'hello world 2',                   :comment => 'long text comes here too'       }     ]  

any solutions structure @user.posts?

inside #edit action :

def edit   #... code   @user.posts << post.new end 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -