Nested association has_many;through doesn't update collection_check_boxes - ruby-on-rails

Using check boxes to update the nested form I can't update the tables. I received following message:
Unpermitted parameter: :category
ActionController::Parameters
{"name"=>"Flux Capacitor", "price"=>"19.55"} permitted: true
I have tried different ways to fix this through the permitted params, including a :category parameter, like so:
def product_params
params.require(:product).permit(:id, :name, :price, :category, categories_attributes: [:id, :name, :category], categorizations_attributes: [:id, :product_id, :category_ids, :category])
end
My models
class Product < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categories, reject_if: proc {|attributes| attributes['name'].blank?}
accepts_nested_attributes_for :categorizations
end
class Categorization < ApplicationRecord
belongs_to :product, inverse_of: :categorizations
belongs_to :category, inverse_of: :categorizations
end
class Category < ApplicationRecord
has_many :categorizations
has_many :products, through: :categorizations, inverse_of: :category
end
class ProductsController < ApplicationController
def edit
#categories = Category.all
end
def new
#product = Product.new
end
def create
#product = Product.new(product_params)
if #product.save
flash[:notice] = 'Product succesfully created'
redirect_to products_path
else
flash[:notice] = 'Product was not created'
render 'edit'
end
end
def update
if #product.update(product_params)
flash[:notice] = "Product succesfully updated"
redirect_to products_path
else
flash[:notice] = 'Product was not updated'
render 'edit'
end
end
app/view/products/edit.html.erb
<%= simple_form_for(#product) do |f| %>
<%= f.input :name %>
<%= f.input :price %>
<%= f.simple_fields_for #product.categories do |cats| %>
<%= cats.collection_check_boxes :ids, Category.all, :id, :name, collection_wrapper_tag: :ul, item_wrapper_tag: :li %>
<% end %>
<%= f.button :submit %>
<% end %>
This seems like something that is common enough that rails and/or simple_form, should provide in a more built-in way to do this. Am I missing something obvious?

If I am understanding you correctly you should be able to do this without the use of accepts_nested_attributes_for or simple_fields_for. Try something like this:
<%= simple_form_for(#product) do |f| %>
<%= f.input :name %>
<%= f.input :price %>
<%= f.association :categories, as: :check_boxes %>
<%= f.button :submit %>
<% end %>
your strong params should look something like this:
def product_params
params.require(:product).permit(:id, :name, :price, { category_ids: [] }])
end

Related

Create controller method with accepts_nested_attributes_for and fields_for

i'm building a web application with Rails 5.2.0 about recipes and I have a doubt about the create method of the controller.
This are my models:
class Recipe < ApplicationRecord
belongs_to :user
has_many :quantities
has_many :ingredients, through: :quantities
accepts_nested_attributes_for :quantities, allow_destroy: true
end
class Quantity < ApplicationRecord
belongs_to :recipe
belongs_to :ingredient
end
class Ingredient < ApplicationRecord
has_many :quantities
has_many :recipes, through: :quantities
end
And here the view to create new recipes:
<%= form_for(#recipe) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
<%= f.label :servings, "Servings" %>
<%= f.number_field :servings %>
<%= f.fields_for :quantities do |quantity| %>
<%= f.hidden_field :_destroy, class: "hidden-field-to-destroy" %>
<%= f.label :ingredient_id, "Ingredient Name" %>
<%= f.text_field :ingredient_id%>
<%= f.label :amount, "Amount" %>
<%= f.number_field :amount %>
<%= f.label :unit, "Unit" %>
<%= f.select(:unit, ["kg","g","l","ml"], {include_blank: true}) %>
<% end %>
<%= f.submit 'Add new recipe' %>
<% end %>
I can add new ingredients dynamically with jquery and also delete them in the same form.
The update method of the controller works perfectly but the create method does not work:
class RecipesController < ApplicationController
def create
#recipe = current_user.recipes.build(recipe_params)
if #recipe.save
flash[:success] = "New recipe created correctly."
redirect_to #recipe
else
render 'new'
end
end
def update
#recipe = Recipe.find(params[:id])
if #recipe.update_attributes(recipe_params)
flash[:success] = "The recipe has been updated correctly."
redirect_to #recipe
else
render 'edit'
end
end
private
def recipe_params
params.require(:recipe).permit( :name, :servings, quantities_attributes: [:ingredient_id, :amount, :unit,:_destroy, :id, :recipe_id])
end
end
I'm trying to do #recipe = current_user.recipes.build(recipe_params) but I get the following error in te view:
Quantities recipe can't be blank
I think this occurs because when trying to create the relation, it is necessary to indicate the recipe_id, but the recipe has not yet been created and the id can not be indicated.
Could you please tell me someone what would be the correct way to create the recipe first and then be able to add the ingredients through Quantity in the create method of the recipe controller?
As per the message shared the qunatity_recipes cannot be blank and you haven't specified any condition to manage this.
Current
class Recipe < ApplicationRecord
belongs_to :user
has_many :quantities
has_many :ingredients, through: :quantities
accepts_nested_attributes_for :quantities, allow_destroy: true
end
Update the accepts nested attributes to allow_nil for Recipe class
class Recipe < ApplicationRecord
belongs_to :user
has_many :quantities
has_many :ingredients, through: :quantities
accepts_nested_attributes_for :quantities, allow_destroy: true, allow_nil: true
end

Rails 5 Has Many Through with Nested Form and Cocoon

Solverd: see bottom
I am building a ordering system of many products, with a HBTM from Join Table to anotherModel, but I have some problems when creating and editing a new record of a nested form.
I have tried many solutions from other questions and followed some tutorial like this or this, but nothing solved my issues.
Product Model:
class Product < ApplicationRecord
belongs_to :subcategory
has_many :order_products, inverse_of: :product
has_many :order, :through => :order_products
end
Order Model:
class Order < ApplicationRecord
has_many :order_products, inverse_of: :order
has_many :products, :through => :order_products
accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true
end
Join Table:
class OrderProduct < ApplicationRecord
belongs_to :order
belongs_to :product
has_and_belongs_to_many :ingredients
accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true
end
Ingredients Model associated with join table: (many-to-many to save added ingredients to every products on the order)
class Ingredient < ApplicationRecord
has_and_belongs_to_many :order_products
end
Order Controller:
# GET /orders/new
def new
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
def order_params
params.require(:order).permit(:subtotal, :discount, :total,
order_products_attributes: [[:id, :order_id, :product_id , :_destroy]])
end
Views:
<%= simple_form_for(#order) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :subtotal %>
<%= f.input :discount %>
<%= f.input :total %>
</div>
<%= f.simple_fields_for :order_products do |o| %>
<%= render 'order_product_fields', f: o %>
<% end %>
<%= link_to_add_association "aggiungi prodotto", f, :order_products %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Partial: (unable to add cocoon's destroy link: undefined method `reflect_on_association' for NilClass:Class due to auto add of blank fields)
<div class="nested-fields order-product-fields">
<%= f.input :product %>
<%= f.input :order %>
<%= f.check_box :_destroy %>
</div>
I get this on console:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PRNzrnxk/jrsAyPK5OzFxix2hIUmjCeWpD8Fkuhuhx8Wp3/xYTbTTvsfQkhnMDEBNnC/iNL46kl68XR7skb03g==", "order"=>{"subtotal"=>"", "discount"=>"", "total"=>"", "order_products"=>{"product"=>"", "order"=>"", "_destroy"=>"0"}}, "commit"=>"Create Order"}
Unpermitted parameter: order_products
My throubles are: EDIT: Solution on each point
When I load order pages, blanks fields for nested attributes are automatically created ()
Was wrong nested_attributes on Order Model:
accepts_nested_attributes_for **:order_products**, reject_if: :all_blank, allow_destroy: true
When submitting form no entry are created. (but I can add product to order via console .ex: order.products << product1)
When I add more product via cocoon's add link, only first is sent to controller
Don't know how to implements Strong Parameters and views for Ingredient's attributes inside the form of Order
Got this working:
Ingredient model:
has_many :ingredient_order_products, inverse_of: :ingredient
has_many :order_products, :through => :ingredient_order_products
IngredientOrderProduct model:
belongs_to :ingredient
belongs_to :order_product
Order Product model:
has_many :ingredient_order_products, inverse_of: :order_product
has_many :ingredients, :through => :ingredient_order_products
accepts_nested_attributes_for :ingredient_order_products, reject_if: :all_blank, allow_destroy: true
Order Controller:
def order_params
params.require(:order).permit(:customer_id, :subtotal, :discount, :total,
order_products_attributes: [[:id, :order_id, :product_id, :qty, :gift, :_destroy,
ingredient_order_products_attributes: [:id, :order_id, :ingredient_id, :_destroy]]])
end
_order_product_fields Partial:
<div id="ingredient">
<%= f.simple_fields_for :ingredient_order_products do |ingredient| %>
<%= render 'ingredient_order_product_fields', f: ingredient %>
<% end %>
<%= link_to_add_association "Aggiungi Ingrediente", f, :ingredient_order_products %>
</div>

Nested Form Gem throws "undefined method"

I'm new to Nested Forms. I'm building a form where Companies can create Promos as under a few Categories. I keep getting undefined method "model_id" for Class. My codes and exact error messages are as below :-
Error Message:-
undefined method `category_id' for #<Company:0x007fd43828b6f8>
<%= f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true %>
<%= render partial: 'promo', locals: {f: f}%>
Models :-
class Company < ActiveRecord::Base
has_many :users, :through => :company_customers
has_many :company_users
has_many :categories, :through => :category_companies
has_many :category_companies
accepts_nested_attributes_for :category_companies
accepts_nested_attributes_for :categories s
end
class CompanyCategory < ActiveRecord::Base
belongs_to :company
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :company_categories
has_many :companies, :through => :company_categories
has_many :promos
accepts_nested_attributes_for :company_categories
end
class Promo < ActiveRecord::Base
belongs_to :category
end
Controller :-
class CompaniesController < ApplicationController
def new
#company = Company.new
end
def create
#company = Company.new(company_params)
respond_to do |format|
if #company.save
format.html { redirect_to #company, notice: 'Company was successfully created.' }
format.json { render :show, status: :created, location: #company }
else
format.html { render :new }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_company
#company = Company.find(params[:id])
end
def company_params
params.require(:company).permit(:company_name, :registration_no, :address, :phone_no, :outlet_location, company_categories_attributes: [:id, :company_id, :category_id, category_attributes:[:id, :name, promo_attribute:[:id, :name, :description]]])
end
end
View:-
<%= form_for(:company) do |f| %>
<div class="medium-6 columns">
<%= f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true %>
<%= render partial: 'promo', locals: {f: f}%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_promo.html.erb
<%= f.fields_for :promos do |promo| %>
<h4 class="text-center">Promotions to be offered</h4><br>
<div class="row">
<div class="medium-6 columns">
<%= f.text_field :name, placeholder: "Name", required: true %>
</div>
<div class="medium-6 columns">
<%= f.text_field :description, placeholder: "Description", required: true %>
</div>
</div>
<% end %>
<p><%= f.link_to_add "Add More Promos", :promos %> </p>
Appreciate the help. Many Thanks !
From the docs, the collection_select has the following use:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
They have this example:
class Post < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :posts
def name_with_initial
"#{first_name.first}. #{last_name}"
end
end
Which would be used like this:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true)
So, when you do:
f.collection_select :category_id, Category.all, :id, :name, {include_blank: 'Choose Category'}, required: true
I think maybe you're missing the object argument? Like, where they have :post.

rails4 collection select with has_many through association and nested model forms

I have a rails4 app. At the moment my collection select only works if I select only one option. Below is my working code. I only have product form. Industry model is populated with seeds.rb. IndustryProduct is only use to connect the other 2 models.
I'd like to know what I have to change in the code to be able to choose more.
I saw some working examples with multiple: true option like (https://www.youtube.com/watch?v=ZNrNGTe2Zqk at 10:20) but in this case the UI is kinda ugly + couldn't pull it off with any of the sample codes. Is there an other solution like having more boxes with one option chosen instead of one box with multiple options?
models:
class Product < ActiveRecord::Base
belongs_to :user
has_many :industry_products
has_many :industries, through: :industry_products
has_many :product_features
accepts_nested_attributes_for :industry_products, allow_destroy: true
accepts_nested_attributes_for :product_features
validates_associated :industry_products
validates_associated :product_features
end
class Industry < ActiveRecord::Base
has_many :industry_products
has_many :products, through: :industry_products
accepts_nested_attributes_for :industry_products
end
class IndustryProduct < ActiveRecord::Base
belongs_to :product
belongs_to :industry
end
_form.html.erb
<%= form_for #product do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
......
<%= f.fields_for :industry_products do |p| %>
<%= p.collection_select :industry_id, Industry.all, :id, :name %>
<% end %>
<%= f.fields_for :product_features do |p| %>
<%= p.text_field :feature, placeholder: "add a feature", class: "form-control" %>
<% end %>
<%= f.submit class: "btn btn-primary" %>
<% end %>
products controller
def new
#product = Product.new
#product.industry_products.build
#product.product_features.build
end
def create
#product = current_user.products.new(product_params)
if #product.save
redirect_to #product
else
render action: :new
end
end
......
def product_params
params.require(:product).permit(....., industry_products_attributes: [:id, :industry_id, :_destroy], industries_attributes: [:id, :name], product_features_attributes: [:feature])
end
Firstly, you could fix your first collection select by using it to set the industry_ids for the #product:
<%= form_for #product do |f| %>
<%= f.collection_select :industry_ids, Industry.all, :id, :name %>
<% end %>
This will allow you to set the collection_singular_ids method, which exists for all has_many associations.
You'd have to back it up in the params method:
#app/controllers/products_controller.rb
....
def product_params
params.require(:product).permit(.... industry_ids: [])
end
A lot more succinct than using nested attributes.
To get that "multiple" selection, you'll want to use the following:
<%= f.collection_select :industry_ids, Industry.all, :id, :name, {}, { multiple: true } %>
Tested & working
--
You may also want to look at collection_check_boxes:
<%= f.collection_check_boxes :industry_ids, Industry.all, :id, :name %>

Rails 4.2 nested form attributes not saving

I cannot seem to get nested attributes to save to the database, though I can see the params in terminal. I am using Rails 4.2.
Here are my models:
class Device < ActiveRecord::Base
belongs_to :hub
has_many :accessories, dependent: :destroy
accepts_nested_attributes_for :accessories,
reject_if: proc { |attributes| attributes['material'].blank? },
allow_destroy: true
end
class Accessory < ActiveRecord::Base
belongs_to :device
end
Here is the controller. I have my device model nested under user and hub model.
class DevicesController < ApplicationController
def edit
#user = User.find_by(params[:user_id])
#hub = Hub.find_by_title(params[:hub_id])
#device = Device.find_by(id: params[:id])
end
def update
#user = User.find_by(params[:user_id])
#hub = Hub.find_by_title(params[:hub_id])
#device = Device.find_by(id: params[:id])
if #device.update_attributes(device_params)
flash[:success] = "update successfully"
redirect_to user_hub_device_path(#user, #hub, #device)
else
render 'edit'
end
end
private
def device_params
params.require(:device).permit(:model, :hub_id, :resolution, :materials, :startcost, :take_online, :delivery_time, :unitcost, :color, :accessories, :accessories_attributes => [:id, :name, :cost, :color, :device_id, :_destroy])
end
end
Finally is my form.
<%= form_for([#user, #hub, #device]) do |f| %>
<fieldset>
<div id="material">
<%= f.fields_for :accessories do |a| %>
<%= render 'devices/accessory', a: a %>
<% end %>
</div>
</fieldset>
The partial:
<div class="row">
<%= a.collection_select :name, Material.all, :material, :material %>
<%= a.text_field :cost, id: "right-label" %>
<%= a.text_field :color, id: "right-label" %>
<%= a.check_box :_destroy %>
</div>
You are whitelisting params[:device][:materials] but you are checking attributes['material'].blank? (note the the s on the end). Which causes the nested attributes to be rejected.

Resources