cIn my Rails 4 app, my update action is working, so I can edit, but my create action is not. I can't figure out why I can add products_id and user_id in the edit form, but when I try to use create a new catalogue I get errors.
My create action:
def create
@catalogue = Catalogue.new(catalogue_params)
if @catalogue.save
redirect_to @catalogue, notice: "Catalogue was successfully created."
else
render action: "new"
end
end
My update action:
def update
if @catalogue.update(catalogue_params)
redirect_to @catalogue, notice: "Catalogue was successfully updated."
else
render action: "edit"
end
end
My strong parameters:
private
def set_catalogue
@catalogue = Catalogue.find(params[:id])
end
def catalogue_params
params.require(:catalogue).permit(:title, :url, :google_name, product_ids: [], user_ids: [])
end
From the form:
Forgot my form code:
.field
= f.collection_select(:user_ids, @catalogue.get_all_users, :id, :name, {}, multiple: true)
.field
= f.label :product_ids
= f.collection_select(:product_ids, @catalogue.get_all_products, :id, :title, {}, multiple: true)
.actions
= f.submit "Save"
The fields that are not working with create are the arrays: product_ids: [] and user_ids: []. If I click on one of the catalogues that I had created with a seed file, I can open it for edit and add a product or user from the drop down fields in the form. The catalogue is updated, no problem. The form has select and sends products and users as arrays.
But if I click on New Catalogue, I get this error:
2 errors prohibited this catalogue from being saved:
Catalogue users is invalid
Catalogue products is invalid
--- !ruby/hash:ActionController::Parameters
utf8: "✓"
authenticity_token: ddJsIAweWHoblmbOAjoNpZ0iPwi8ookrgH6HOzd/jh4=
catalogue: !ruby/hash:ActionController::Parameters
title: Testing Create
url: www.something.com
google_name: ''
user_ids:
- ''
- '1'
product_ids:
- ''
- '1'
commit: Save
action: create
controller: catalogue
I can see that the selections are there for each item but not the names, so I don't understand why it's not creating, and why if I open a previously created catalogue, I can add products and users via the form. But not create.
And this from the console:
Processing by CatalogueController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ddJsIAweWHoblmbOAjoNpZ0iPwi8ookrgH6HOzd/jh4=", "catalogue"=>{"title"=>"Testing Create", "url"=>"www.something.com", "google_name"=>"", "user_ids"=>["", "1"], "product_ids"=>["", "1"]}, "commit"=>"Save"}
I am using join tables between user and catalogue, and product and catalogue:
class CatalogueProduct < ActiveRecord::Base
belongs_to :catalogue
belongs_to :product
validates :product_id, :catalogue_id, presence: true
end
class Catalogue < ActiveRecord::Base
include ModelHelper
has_many :catalogue_users, dependent: :destroy
has_many :users, through: :catalogue_users
has_many :catalogue_products, dependent: :destroy
has_many :products, through: :catalogue_products
The user model follows the same patter as above.
I'm wondering if I need to convert those arrays to strings? Because in the above it's listing the actual id. But why then doesn't the update action need the conversion? I'm confused about what's going on and how to fix it. I kept getting errors when I tried converting to strings in the strong parameters.
Thank you!!!!!