3

So I have an interesting problem I'm working on. I am trying to create multiple objects of the same model in one view. I would like to display all the possible objects in my view, check boxes to select which ones to create, then submit and create all the corresponding objects.

Now the objects to select are gotten using an API request and returned in JSON format. The JSON is then displayed on the view for the user to select, then an array containing all the selected objects is sent back to the controller for creation.

Here is the relevant code that I've tried so far.

objects_controller.rb

  def new
    @possible_objects = <api call to get objs>
    @objects = []
  end

  def create   
    params[:objects].each do |obj|  
      # create and save obj
    end  
  end

objects/new.html.erb

<% form_for @objects do |f| %>  
    <% @possible_objects.each do |api_obj| %>
        <%= check_box_tag(api_obj["name"])%>
        <%= api_obj["name"] %>  
    <% end %>       
    <%= f.submit %> 
<% end %>

This is definitely not the right approach, as the form will not accept an empty array as a parameter. I'm not sure where else to go with this, any pointers in the right direction would be great. Thanks.

Thanks to MrYoshiji for pointing me in the right direction, this is what ended up working

objects_controller.rb

def 
  @possible_objects = <api call to get objs>
end

def create
  params[:objects].each do |object|
    new_obj = Object_Model.new( <params> )
    new_obj.save
    if !new_obj.save
      redirect_to <path>, alert: new_obj.errors.full_messages and return
    end
  end
  redirect_to <path>, notice: 'Successfully created.'
end

objects/new.html.erb

<%= form_tag objects_path(method: :post) do %>
  <% @possible_objects.each do |api_obj| %>
    <%= check_box_tag 'objects[]', api_obj %>
    <%= possible_object["name"] %>
  <% end %>
  <%= submit_tag 'Create'%> 
<% end %>

1 Answer 1

4

Can you try the following?

# view
<% form_tag my_objects_path(method: :post) do |f| %>  
    <% @possible_objects.each do |api_obj| %>
        <%= check_box_tag 'objects[names][]', api_obj["name"] %>
        <%= api_obj["name"] %>  
    <% end %>
    <%= f.submit %> 
<% end %>

# controller
def create   
  params[:objects][:names].each do |obj_name|  
    YourModelForObject.create(name: obj_name)
  end  
end

See this comment on the documentation of check_box_tag: http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag#64-Pass-id-collections-with-check-box-tags

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. Unfortunately, I'm still getting the error "First argument in form cannot contain nil or be empty", which is due to passing @objects to form_for. There is something inherently wrong with attempting to pass an empty @objects array to the view with the intent to populate it client side.
That is because the form_for is defined for an object, but you give it an empty array. Use form_tag instead (I'll update my answer in a sec @AnthonyTo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.