Reputation: 918
I am building a form that launches a new Campaign. With this Campaign, you can choose a Zip file and/or some PDF files. Zip and PDF files are all stored in an Uploads table.
So far, it appears I can choose a Zip file, however, once I add the collection_check_box
for PDFs, create a Campaign, I receive the following error...
expected Array (got String) for param `upload_ids'
My form looks like this..
<%= form_for @campaign, url: {action: "create"} do |f| %>
<%= f.label :name, class: "right-label" %>
<%= f.text_field :name %>
<%= f.label :comment, "Comments" %>
<%= f.text_area :comment %>
<%= f.label :zip_file, class: "right-label" %>
<%= f.collection_select :upload_ids, Upload.where(file_type: ".zip"), :id, :file_name, include_blank: "Include a Zip File" do |b| %>
<%= b.option + File.basename(b.text, '.*') %>
<% end %>
<%= f.label :pdf_files %>
<ul><%= f.collection_check_boxes :upload_ids, Upload.where(file_type: ".pdf"), :id, :file_name do |b| %>
<li><%= b.label do %>
<%= b.check_box + File.basename(b.text,'.*') %>
</li>
<% end %>
<% end %>
</ul>
Here is a the error displayed on the server as requested.
[2015-06-01 14:50:57] ERROR Rack::Utils::ParameterTypeError: expected Array (got String) for param `upload_ids'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/utils.rb:142:in `normalize_params'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/utils.rb:156:in `normalize_params'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/utils.rb:117:in `block in parse_nested_query'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/utils.rb:114:in `each'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/utils.rb:114:in `parse_nested_query'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/request.rb:369:in `parse_query'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/request.rb:215:in `POST'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:39:in `method_override_param'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:27:in `method_override'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:15:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/runtime.rb:18:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/lock.rb:17:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/sendfile.rb:113:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/lock.rb:17:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/content_length.rb:15:in `call'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/handler/webrick.rb:89:in `service'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/Josh/.rbenv/versions/2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
EDIT
If I delete one of the collection blocks (zip or pdf), my Campaign creation is successful.
It appears as if there is a conflict with my "upload_ids" on both the Zip file and PDF file selection methods above. Even though my model relationship states that a Campaign has_many: uploads, through: uploadable
and has_many: uploadable
, it's trying to accept both - the Zip and PDF/s - one instance of upload_id
.
I'm still unsure how to get around this. Any help is appreciated.
controllers/campaigns_controller.rb
class CampaignsController < ApplicationController
def index
@campaigns = Campaign.all.order("created_at DESC")
end
def new
@campaign = Campaign.new
end
def create
@campaign = Campaign.new(campaign_params)
if @campaign.save
flash[:success] = "Campaign Successfully Launched!"
redirect_to @campaign
else
flash[:error] = "There was a problem launching your Campaign."
redirect_to new_campaign_path
end
end
def show
@campaign = Campaign.find(params[:id])
end
private
def campaign_params
params.require(:campaign).permit(:name, :comment)
end
end
log/development.log
Started GET "/campaigns/new" for ::1 at 2015-06-01 18:54:19 -0700
Processing by CampaignsController#new as HTML
[1m[35mUpload Load (0.2ms)[0m SELECT "uploads".* FROM "uploads"
[1m[36mUpload Load (0.3ms)[0m [1mSELECT "uploads".* FROM "uploads" WHERE "uploads"."file_type" = $1[0m [["file_type", ".zip"]]
[1m[35mUpload Load (0.1ms)[0m SELECT "uploads".* FROM "uploads" WHERE "uploads"."file_type" = $1 [["file_type", ".pdf"]]
Rendered campaigns/_form.html.erb (6.6ms)
Rendered campaigns/new.html.erb within layouts/application (7.5ms)
Rendered layouts/_header.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 82ms (Views: 80.4ms | ActiveRecord: 0.5ms)
Update #1
Another Developer friend noted that I cannot use "upload_ids" for both collection_selects
. That is also apparent in my experiments above. So how do I allow a user to associate the object with 2 other objects from a separate model/table?
I changed one collection_select
object param to be uploadable_ids
but that is a random name I placed. I have no clue what to put as that parameter. Now I receive the flash[:error] "There was a problem launching your Campaign."
Update #2
I've tried many suggestions including the ones on this posting but still struggling to get this to work. The select_tag
throws and undefined method
so for the time being, I'm simply using select
. I tried adding the upload_ids[]
yet was thrown an error that there needed to be more arguments.
I'm also confused on whether I need to add anything to my controller. Will my Active Record associations be enough when I decide to query which Campaign is associated with which files?
Here is the zip and pdf portion of the form in its current state..
<%= f.label :data_file, class: "right-label" %>
<%= f.select :upload_ids,
options_from_collection_for_select(
Upload.where(file_type: ".zip"), :id, :file_name),
{include_blank: "Include a Zip File"}, {mulitple: true} %>
<%= f.label :additional_files %>
<%= f.collection_check_boxes(:upload_ids,
Upload.where(file_type: ".pdf"), :id, :file_name,
{ mulitple: true }) do |b| %>
<%= b.label do %>
<%= b.check_box + File.basename(b.text,'.*') %>
<% end %>
<% end %>
If it is of any added help, here the model relationships:
class Campaign < ActiveRecord::Base
has_many has_many :uploadable
has_many :uploads, through: :uploadable
end
class Uploadable < ActiveRecord::Base
belongs_to :campaign
belongs_to :upload
end
class Upload < ActiveRecord::Base
has_many :uploadable
has_many :campaigns, through: :uploadable
end
Upvotes: 1
Views: 662
Reputation: 84172
The main issue I see is that your checkboxes are submitting an array parameter (you can select multiple check boxes) but your collection_select
is trying to submit a singular value. The mixing of the two is what causes the Rack::Utils::ParameterTypeError
Your uploads
association is a has_many so using an array parameter is the right thing to do. The simplest possible thing you can do is make the select a multiple choice one, by adding the multiple: true
option. Note that this must go in a separate hash to your include_blank
option.
To keep a separate select I think you'll need to drop down to calling select_tag and specify upload_ids[]
as the input name. You can still use options_from_collection_for_select
to generate the option tags.
Upvotes: 1