1

I'm using this good answer:

https://stackoverflow.com/a/16920211/4412054

in my project:

_form.html.erb:

= form_tag({controller: "users", action: "create"}, remote: true) do
  %table
    %tr
      %td= text_field_tag 'user[][first_name]'
      %td= text_field_tag 'user[][last_name]'

    %tr.actions
      %td= submit_tag 'Save'
      %td= button_tag 'Add new user form', id: 'add_user_form'

    %tr.new_user_row.hidden # hidden class matches the css rule: {display:none;}
      %td= text_field_tag "user[][first_name]"
      %td= text_field_tag "user[][last_name]"

:javascript # jQuery
  $('#add_user_form').bind('click', function(e) {
    var row = $('tr.new_user_row').clone().removeClass('hidden new_user_row');
    $('tr.actions').before(row); # will append the <tr> before the actions
  });

and UsersController:

def create
  params[:user].each do |attr|
    User.create(attr)
  end
end

but when I submit it give me this error:

ActiveModel::ForbiddenAttributesError

If I disable Rails 4 strong_parameters by config.action_controller.permit_all_parameters = true in config/application.rb it works and save.

How to permit the hash

..., "user"=>[{"first_name"=>"Name", "last_name"=>"Last"}, {"first_name"=>"", "last_name"=>""}],....

in my controller?

UPDATE

If I use in Rails console:

User.create({"first_name"=>"Name", "last_name"=>"Last"})

it works!!!

UPDATE 2

In my controller I have this:

def users_params
  params.require(:user).permit(:user, array: [:first_name, :last_name])
end
Community
  • 1
  • 1

1 Answers1

0
params.permit(:user, array: [
  :last_name,
  :first_name,
])

From: https://stackoverflow.com/a/30285592/484130

UPDATE:

class FooController < ApplicationController

  def users_params
    params.permit(user: [:last_name, :first_name])["user"]
  end

  def some_action
    @users = Array.new

    users_params.each do |user_params|
      user = User.new user_params
      @users.push user if user.save
    end

    respond_to do |format|
      format.json { render json: @users }
    end
  end

  ....

end

This should work.
And sorry for not test it myself..

BTW, gem byebug is your best friend.

Community
  • 1
  • 1
Francis.TM
  • 1,761
  • 1
  • 18
  • 19
  • Nothing, again: `Completed 500 Internal Server Error in 129ms (ActiveRecord: 36.2ms) ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError: activemodel (4.2.5) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment' activerecord (4.2.5) lib/active_record/attribute_assignment.rb:33:in `assign_attributes'` –  Feb 17 '16 at 16:44
  • @JohnSam I have tested and updated the answer, sorry for previous one which I didn't test it myself. – Francis.TM Feb 17 '16 at 17:16
  • Dear @Francis.TM, here we are again in trouble. Like before: `ActiveModel::ForbiddenAttributesError - ActiveModel::ForbiddenAttributesError:` –  Feb 17 '16 at 17:18
  • @JohnSam I have give a full example that which I have tested. – Francis.TM Feb 17 '16 at 17:25
  • Now it works. Thanks, but why I have to use this strange code `params.permit(user: [:last_name, :first_name])["user"]`? I can't use simple and standard `def user_params params.require(:user).permit(user: [:last_name, :first_name]) end`?? –  Feb 17 '16 at 17:35
  • @JohnSam Rails 4 don't let you use form params directly into a db query, you need permit it first. so we need a `params.permit`, just like a whitelist. detail you can check http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit . – Francis.TM Feb 17 '16 at 17:38
  • How can I check if at least one of them is saved? Other words how can I use the standard: `respond_to do |format| if @user.save.........` –  Feb 17 '16 at 17:51
  • Dear @Francis.TM, everything works now. Just one question now. How can I group by :lastname, or `:firstname` and save records with sum of every row grouped by one attribute and an attribute "count" (count column is there already in database, but I need one record total for the same lastname in `@users`)? Sorry the mess! –  Feb 17 '16 at 18:48
  • And then a curiosity, instead of save every each per time, how to save one time with a big INSERT query? –  Feb 17 '16 at 20:23
  • 1
    @JohnSam You can see https://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/ for mass inserting information. – Francis.TM Feb 18 '16 at 04:10