ruby on rails - cocoon gem not passing parameters -
so although have used cocoon before, i'm still relatively new , whatever reason can't seem pass through parameters (i've tried 2 different applications , on fedora 22 , mac 10.10.4).
for context i've been using standard rails forms in .erb , have both :post , :post_items have description. i'm using cocoon 1.2.6.
so in application.js have
//= require cocoon
in post.rb
class post < activerecord::base has_many :post_items accepts_nested_attributes_for :post_items, :reject_if => :all_blank, :allow_destroy => true validates :title, :post_items, presence: true validates :title, length: {maximum: 255} end
in post_item.rb
class postitem < activerecord::base belongs_to :post end
and relevant controllers (posts_controller.rb):
def index @post = post.new end def create params[:post][:ip_address] = request.remote_ip @post = post.create(post_params) if @post.save flash[:success] = "your post created" redirect_to @post else flash[:failure] = "there error saving post" render 'index' end end private def find_post @post = post.find(params[:id]) end def post_params params.require(:post).permit(:title, :tags, :hits, :ip_address, post_items_attributes: [:id, :description, :_destroy]) end
and in views have (_form.html.erb)
<%= form_for @post, html: {multipart: true} |f| %> <% if @post.errors.any? %> <p>there <%= @post.errors.count %> errors</p> <% @post.errors.full_messages.each |msg| %> <p><%= msg %></p> <% end %> <% end %> <!-- bunch of fields here --> <div class="six columns"> <h3>add images</h3> <div id="post_items"> <% f.fields_for :post_items |post_item| %> <%= render 'post_item_fields', f: post_item %> <% end %> <div class="links"> <%= link_to_add_association 'add image', f, :post_items, class: "add-button" %> </div> </div> <%= f.button :submit, class: "button submit-button" %> <% end %>
and lastly _post_item_fields.html.erb
<div class="nested-fields"> <div class="field"> <%= f.label :description %> <%= f.text_area :description %> </div> <%= link_to_remove_association "remove", f %> </div>
there css files modify html have no js other what's automatically built. i've literally been stuck on problem days , can't figure out what's wrong. have tried post.rb without validates code still doesn't work.
the lead have when print out parameters has dropped post_items parameters , life of me can't figure out why.
thanks help!
edit: sake of information i've added migrations.
in db/migrate/20150722191917_create_post_items.rb
class createpostitems < activerecord::migration def change create_table :post_items |t| t.string :description t.belongs_to :post, index: true, foreign_key: true end end end
and in db/migrate/20150722173629_create_posts.rb
class createposts < activerecord::migration def change create_table :posts |t| t.string :title t.string :tags t.integer :hits, null: false, default: 0 t.string :ip_address t.timestamps null: false end add_index :posts, :ip_address end end
also if matters didn't use scaffold when generating them
well think figured out what's wrong (although don't understand we're supposed around it) once got rid of div class="six columns" worked, apparently interfering cocoon gems ability parse html.
thanks help!
edit: clarify had change _form.html.erb to:
<%= form_for @post, html: {multipart: true} |f| %> <% if @post.errors.any? %> <p>there <%= @post.errors.count %> errors</p> <% @post.errors.full_messages.each |msg| %> <p><%= msg %></p> <% end %> <% end %> <!-- bunch of fields here --> <h3>add images</h3> <div id="post_items"> <% f.fields_for :post_items |post_item| %> <%= render 'post_item_fields', f: post_item %> <% end %> <div class="links"> <%= link_to_add_association 'add image', f, :post_items, class: "add-button" %> </div> </div> <%= f.button :submit, class: "button submit-button" %> <% end %>
so guess little fault error have been easier spot if had used full html instead of parsing cocoon-relevant bits there regardless. (basically moral of story can't have div's inside of form)
Comments
Post a Comment