Need help with cocoon

hi,

cocoon gem is working fine, but i need to add a few parameters / attributes which should not be visible to the user, eg:

lets say project <has_many> tasks

how would i add the user_id to the tasks? i dont want to do it in the view…

i basically have a multitenant setup, and need to set a few more attributes from other tables, but dont want to show them in the view…

thx

You can add it to the input using a hidden field, or you can merge it into the params in the controller action.

thank you. did that and it works. now, i have the second challenge: the main form gets saved via ajax, and within the controller i check whether ist a create or update flow - BUT, the has_many records (tasks)always get created, instead of updating the existing ones.

here my controller code: >> how can i get the has_mnay records to get updated instead of newly added / created?

if params[:record][:id].blank?

        p "NEW mode....#{m}"

        @new_rec = m.constantize.new(params[:record].except(:company_id,:company))

        @new_rec.company_id = @cid if @new_rec.respond_to?("company_id") 

        @new_rec.created_by = current_user.id if @new_rec.respond_to?("created_by") 

        @new_rec.user_id = current_user.id if @new_rec.respond_to?("user_id") 

      else

        p "update mode...."

        @is_update = true

        @new_rec = m.constantize.find(params[:record][:id])

        p @new_rec

        @new_rec.assign_attributes(params[:record].except(:id,:company_id,:company))

        @new_rec.updated_by = current_user.id if @new_rec.respond_to?("updated_by") 

        p @new_rec

      end

Couple things

Don’t use 1 controller action to perform 2 operations, i.e. create/update. Keep them separate and long term maintenance will be easier

Use nested_attributes_for in your Project model and you don’t have to do any of your find/constantize stuff in your actions. A simple project.update(params[:project]) would handle the creation/update/deletion of your nested tasks records