Rails 5 Has Many Through with Nested Form and Cocoon - ruby-on-rails

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>

Related

Nested association has_many;through doesn't update collection_check_boxes

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

Having problems saving users' responses in quiz form

I'm trying to create an app where a user can click on a quiz and it will take them to the page where they can answer the quiz questions and submit their responses to a results page.
I'm having trouble saving the users' responses - it's either only saving the last records rather than all of them or not saving to the database at all.
Here is my code:
Question.rb:
belongs_to :quiz
has_many :answered_questions, dependent: :destroy
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true
Answer.rb:
belongs_to :question
has_many :answered_questions, dependent: :destroy
User.rb:
has_many :answered_questions, dependent: :destroy
accepts_nested_attributes_for :answered_questions
AnsweredQuestion.rb:
belongs_to :user
belongs_to :question
belongs_to :answer
belongs_to :quiz
Quiz.rb
has_many :questions, dependent: :destroy
has_many :answered_questions, through: :questions, dependent: :destroy
accepts_nested_attributes_for :answered_questions, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
quizzes_controller.rb:
class QuizzesController < ApplicationController
before_action :user_completed_quiz, only: [:show]
def show
#quiz = Quiz.find(params[:id])
#questions = Question.all
#answered_questions = current_user.answered_questions.build
#quiz.answered_questions.build
end
def create
#quiz = Quiz.new(show_params)
if #quiz.save
flash[:success] = "You have created a new quiz!"
redirect_to #quiz
else
render 'new'
end
end
def results
#quiz = Quiz.find(params[:id])
end
def post_answered_questions
#quiz = Quiz.find(params[:quiz][:id])
#answered_question = #quiz.answered_questions.build(show_params)
if #answered_question.save
flash[:success] = "You have completed the quiz!"
redirect_to results_quiz_path(params[:quiz][:id])
else
render ''
end
end
private
def user_completed_quiz
if(current_user.answered_questions.pluck(:quiz_id).uniq.include?(params[:id].to_i))
redirect_to quizzes_path
end
end
def show_params
params.require(:quiz).permit(:title, answered_questions_attributes: [:id, :answer_id, :question_id, :user_id, :quiz_id], questions_attributes: [:id, :question_title, :quiz_id, :done, :_destroy, answers_attributes: [:id, :answer_title, :question_id, :quiz_id, :correct_answer, :_destroy]])
end
end
show.html.erb - quizzes (where the form is):
<%= form_for(#answered_questions, url: answered_questions_path, method: "POST") do |f| %>
<%= #quiz.title %>
<%= f.hidden_field :id, :value => #quiz.id %>
<% #quiz.questions.each do |question| %>
<%= f.fields_for :answered_questions do |answer_ques| %>
<h4><%= question.question_title %></h4>
<%= answer_ques.hidden_field :question_id, :value => question.id %>
<%= answer_ques.hidden_field :quiz_id, :value => #quiz.id %>
<%= answer_ques.select(:answer_id, options_for_select(question.answers.map{|q| [q.answer_title, q.id]})) %>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>

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 4: Being able to select multiple instances of one model when creating the model it has a has_many :through relationship with

When I add a new Book to my Library, I want to be able to specify what Shelves (note: "Shelf" is the term I used in my app for the model that groups Books together within a Library; a better name for this model would have been Category or Genre, but I was stupid) in that Library it should belong to. Right now, I'm able to add a Book to my Library, but I'm having issues with the Shelves. I'm pretty new at learning Rails and I'd appreciate any help.
Models
user.rb:
class User < ActiveRecord::Base
has_one :library, dependent: :destroy
...
end
library.rb:
class Library < ActiveRecord::Base
belongs_to :user
has_many :shelves, dependent: :destroy
has_many :catalogs, dependent: :destroy
has_many :books, :through => :catalogs, dependent: :destroy
...
end
book.rb:
class Book < ActiveRecord::Base
has_many :catalogs, dependent: :destroy
has_many :libraries, :through => :catalogs, dependent: :destroy
has_many :bookshelves, dependent: :destroy
has_many :shelves, :through => :bookshelves, dependent: :destroy
...
end
catalog.rb:
class Catalog < ActiveRecord::Base
belongs_to :book
belongs_to :library
end
shelf.rb:
class Shelf < ActiveRecord::Base
belongs_to :library
has_many :bookshelves, dependent: :destroy
has_many :books, :through => :bookshelves, dependent: :destroy
...
end
bookshelf.rb
class Bookshelf < ActiveRecord::Base
belongs_to :book
belongs_to :shelf
end
Controller
books_controller.rb
class BooksController < ApplicationController
...
def create
#book = Book.new(book_params)
#library = current_user.library
if #book.save
#book.catalogs.create(:library_id => #library.id)
flash[:success] = "Book added to library!"
redirect_to current_user
else
render 'current_user'
end
end
...
private
def book_params
params.require(:book).permit(:title, :author, :publisher, :isbn)
end
...
end
View
_book_form.html.erb:
<%= form_for(#book) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :title, placeholder: "Book Title" %>
<%= f.text_field :author, placeholder: "Author" %>
<%= f.text_field :publisher, placeholder: "Publisher" %>
<%= f.text_field :isbn, placeholder: "ISBN" %>
<%= f.fields_for :catalogs do |ff| %>
<%= ff.hidden_field :library_id %>
<% end %>
<%= f.fields_for :bookshelves do |ff| %>
<%= ff.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>
<% end %>
</div>
<%= f.submit "Add Book to Library", class: "btn btn-primary" %>
<% end %>
In your book_params method, you don't have shelf_ids listed. So it won't be passed into the .new method and thus it won't be saved. Since you wan't multiple ids, you want to do this:
def book_params
params.require(:book).permit(:title, :author, :publisher, :isbn, shelf_ids: [])
end
That way, the controller knows it's an array and not just a single value.
Oh, and btw, shelf_ids and any other array values like that must be at the end or it will throw an error.
Edit:
Also change this part of code:
<%= f.fields_for :bookshelves do |ff| %>
<%= ff.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>
<% end %>
to this:
<%= f.collection_select :shelf_ids, current_user.library.shelves.all.collect {|x| [x.name, x.id]}, {}, multiple: true %>
You don't need to worry about the through part of the association. Just set up the has_many part and it'll work.

Ruby on Rails unpermitted parameters with has many through

I have order and products, and a join table called orders_products, the order has many products through order_products and accepts nested attributed for it.
When I try to save it keeps saying unpermitted paramters: order_product
Params
def order_params
params.require(:order).permit(:id, :order_number, :customer_id, {order_products_attributes: [:id, :order, :product, :quantity ]}, {:product_ids => []})
end
Order model
class Order < ActiveRecord::Base
belongs_to :customer
has_many :order_products, class_name: "OrderProduct"
has_many :products, through: :order_products
accepts_nested_attributes_for :order_products, :allow_destroy => true
end
Order Product model
class OrderProduct < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
Order controller new action
def new
#order = Order.new
#order.order_products.build
end
Order Form
<%= simple_form_for #order do |f| %>
<%= f.input :order_number %>
<%= f.fields_for :order_product do |fa| %>
<%= fa.input :product, collection: Product.all %>
<%= fa.input :quantity %>
<% end %>
<%= f.association :customer, as: :select %>
<%= f.submit %>
<% end %>
Params hash - {"utf8"=>"√","authenticity_token"=>"yBrH91u0OHTSPnCFO/484Ff6CRtyRLSg5AKD1Lc33k4=", "order"=>{"order_number"=>"0121", "order_product"=>{"product"=>"4", "quantity"=>"5"}, "customer_id"=>"3"}, "commit"=>"Create Order"}
Unpermitted parameters: order_product
you are missing s here:
<%= f.fields_for :order_products do |fa| %>

Resources