Unpermitted parameter simple_form - ruby-on-rails

I am trying to create a nested form with Simple_fields in ruby 4.
However, every time i try to enter data into the form I get a unpermitted parameter error in the server console after trying to submit.
I already tried the solutions found in the simple_form wiki and did some testing, but that doesn't seem to work.
The _form:
<%= simple_form_for(#enquiry) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<H1>Algemene informatie</H1>
<%= f.input :reference, placeholder: 'Referentie' %>
<br>
<%= f.label :Locatie %>
<%= f.select :location, [['Chemiepark', 'chemiepark'], ['Farmsum', 'farmsum'], ['Winschoten', 'winschoten']] %>
<br>
<%= f.input :description, placeholder: 'Omschrijving' %>
<br>
<%= f.input :date %>
<br>
<%= f.input :amount, placeholder: 'Aantal' %>
</div>
<hr>
<% if false %>
<div class="form-inputs">
<%= f.simple_fields_for :enquiry_measures do |e| %>
<H1>Maatregelen</H1>
<%= e.input :responsible, placeholder: 'Verantwoordelijke' %>
<br>
<%# e.input :needed, as: :check_boxes,
collection: ["ja", "nee"] %>
<% end %>
<br>
</div>
<% end %>
<div class="form-inputs">
<%= f.simple_fields_for :tools do |t| %>
<% #enquiry.tools.each do |tool| %>
<%= field_set_tag 'Tool' do %>
<%= f.simple_fields_for "tool_attributes[]", tool do |tf| %>
<h1>Gereedschappen</h1>
<br>
<%= tf.input :handtool, placeholder: 'Handgereedschap' %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
The strong attributes plus what i tested:
def enquiry_params
# was gegenereerd door de scaffold params.fetch(:enquiry, {})
params.require(:enquiry).permit(:reference, :location, :description, :date, :amount,
:enquiry_measures_attributes => [:done, :responsible, :needed], :tools_attributes => [:handtool] )
#:enquiry_measures_attributes => [:done, :responsible, :needed])
#enquiry_measure_attributes: [:done, :responsible, :needed] )
update
code from models
class Enquiry < ActiveRecord::Base
#ophalen van andere tabellen voor het formulier. Has_many is 1 op veel relatie
#accepts_nested_attributes Nested attributes allow you to save attributes on associated records through the paren
# de dere regel zorgt ervoor dat de maatregelen worden opgehaald via de tussentabel enquiry_measures.
has_many :enquiry_measures, :class_name => 'EnquiryMeasure' #, inverse_of: :Enquiry
accepts_nested_attributes_for :enquiry_measures, :allow_destroy => true
has_many :measures, -> { uniq }, :class_name => 'Measure', :through => :enquiry_measures, dependent: :destroy
accepts_nested_attributes_for :measures, :allow_destroy => false
has_many :controls, :class_name => 'Control' #, inverse_of: :Enquiry
has_many :applicants, :class_name => 'Applicant' #, inverse_of: :Enquiry
has_many :agrees, :class_name => 'Agree' #, inverse_of: :Enquiry
has_many :signatures, :class_name => 'Signature' #, inverse_of: :Enquiry
accepts_nested_attributes_for :signatures, :allow_destroy => false
has_many :tools, :class_name => 'Tool', :dependent => :destroy #, inverse_of: :Enquiry
accepts_nested_attributes_for :tools, :allow_destroy => true
#:dependent => :destroy zorgt ervoor dat de foreign record ook word verwijderd.
#de instances van andere tabellen:
e = Enquiry.new
e.enquiry_measures.build(:enquiry_id => :id)
e.measures.build
# 28-11 MG de pagina's die in het form worden gebruikt.
cattr_accessor :form_steps do
%w(basic when measurements tool)
end
attr_accessor :form_step
validates :reference, presence: true, if: -> { required_for_step?(:basic) }
validates :amount, :date, presence: true, if: -> { required_for_step?(:when) }
#validates :needed, presence: true, if: -> { required_for_step?(:measurements) }
def required_for_step?(step)
return true if form_step.nil?
return true if self.form_steps.index(step.to_s) <= self.form_steps.index(form_step)
end
#voor het mailen met behulp van de mailgem:
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "My Contact Form",
:to => "marco.groenhof#jpbgroep.nl",
:from => %("#{name}" <#{email}>)
}
end
end
and 1 of the related models: in this case enquiry_measure
class EnquiryMeasure < ActiveRecord::Base
belongs_to :enquiry
validates_presence_of :enquiry
has_many :measure
#serialize zodat de data uit de collection select met multiple: true op kan worden geslagen.
serialize :measure
end
and tools:
class Tool < ActiveRecord::Base
belongs_to :enquiry, :class_name => 'Enquiry' #, inverse_of: :applicant
validates_presence_of :enquiry
end
I know class_name is not really needed anymore.
UPDATE
The logging:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"i3YukMoOaYEuUdxk6kmhoQ5q9uLQHHISW+NAU/L+kNjSwMZERmdIIVXZvJUh0vTnEPidaMvMEIlVT/aTlkTNPw==", "enquiry"=>{"reference"=>"Test", "location"=>"chemiepark", "description"=>"3ro0qjhrojeofj", "date(1i)"=>"2017", "date(2i)"=>"1", "date(3i)"=>"3", "amount"=>"2", "tools_attributes"=>{"0"=>{"handtool"=>"Hamer"}}}, "commit"=>"Create Enquiry"}
The only weird thing i see is the "tools_attributes"=>{"0"=>{"handtool"=>"Hamer"}}}
Why is that 0 there? Could it be the id, because that would make sense to why i can not save.
And just to make sure, this is the tool tabel and foreign key:
add_index "tools", ["enquiry_id"], name: "index_tools_on_enquiry_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "name", limit: 255
t.string "email", limit: 255
t.string "password_digest", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "tools", "enquiries"
UPDATE 4/01
Just installed rails panel to see what that would say, but i keep thinking the problem is within the parameters:
{"reference":"test","location":"chemiepark","description":"iwopjf","date(1i)":"2017","date(2i)":"1","date(3i)":"4","amount":"2","tools_attributes":{"0":{"handtool":"hamer"}}}
Why does it keep sending that 0? i suspect it to be the tool id, which would declare the not being able to save.

Try making you strong parameter as
params.require(:enquiry).permit(:reference, :location, :description, :date, :amount, enquiry_measures_attributes: [:id, :done, :responsible, :needed, :_destroy], tools_attributes: [:id, :handtool, :_destroy] )
And your model that is being nested should be something as below. Try doing this once.
class Tool < ActiveRecord::Base
# For a while let's not have any validation.
end
Hope this will work in your case as I fix it for my own.

I decided to updatew this question as i got the answer that helped me. It may not be the completely correct answer, but i managed to solve my issue by using the default form_for in uby on Rails. A bit more coding work, but it does work.

Related

How to manage many to many relation for nested fields in Ruby on Rails

class Resume < ActiveRecord::Base
has_many :user_skills, :dependent => :destroy
accepts_nested_attributes_for :user_skills, :allow_destroy => true, :reject_if => :all_blank
end
class UserSkill < ActiveRecord::Base
belongs_to :resume
has_and_belongs_to_many :technologies
end
class Technology < ActiveRecord::Base
has_and_belongs_to_many :user_skills
end
<%= nested_form_for([:student, #resume], validate: true, :html => { :multipart => true, class: "full-width" }) do |f| %>
------------------------------
Resume fields
------------------------------
<h5>User Skills</h5>
<%= f.fields_for :user_skills do |us| %>
<%= us.label :academic_years, "Academic Years" %>
<%= us.text_field :academic_years %>
<%= us.label :professional_years, "Professional Years" %>
<%= us.text_field :professional_years %>
<%= us.fields_for :technologies do |tech| %>
<%= tech.collection_select :name, Technology.all, :id, :name, { prompt: "Select Technology"}, { :multiple => true, :size => 10} %>
<% end %>
<%= us.link_to_remove "Remove", class: "btn btn-small red right" %>
Now I don't know how I manage this record in controller for create and update, And also I don't know how will I show this records.... If you understand my problem then pleasr provide me controller code for update and create of resume controller and also help me to show the resume data.
I think you use old nested_form gem by Ryan Bates. You should use newest for example simple_form or others from ruby-toolbox.com

Rails 4 Polymorphic associations and concerns

I'm trying to add an Evaluation model to my Rails 4 app.
I have made a model called evaluation.rb. It has:
class Evaluation < ActiveRecord::Base
belongs_to :evaluator, :polymorphic => true
belongs_to :evaluatable, :polymorphic => true
I have also made concerns for evaluator and evaluatable as:
module Evaluator
extend ActiveSupport::Concern
included do
has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation'
end
end
module Evaluatable
extend ActiveSupport::Concern
included do
has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name: 'Evaluation'
end
end
I have included each concern in my user model:
class User < ActiveRecord::Base
include Evaluator
include Evaluatable
In my show page, I want to show a particular user's evaluations (received from other users -who are evaluators).
In my show, I have:
<% Evaluation.find(params[:id]).evaluations.order('created_at DESC').each do |eval| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<%= eval.remark %>
<%= eval.personal_score %>
<small><%= eval.created_at %></small>
In my evaluations form, I"m not sure how to designate the recipient of the evaluation. I have made the basic form, but I'm not clear about how to tie it to the user who should receive the evaluation.
<%= simple_form_for(#evaluation) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %>
<%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %>
My evaluations table has:
t.integer "user_id"
t.integer "evaluatable_id"
t.string "evaluatable_type"
t.integer "overall_score"
t.integer "project_score"
t.integer "personal_score"
t.text "remark"
t.boolean "work_again?"
t.boolean "continue_project?"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "evaluations", ["evaluatable_type", "evaluatable_id"], name: "index_evaluations_on_evaluatable_type_and_evaluatable_id", unique: true, using: :btree
QUESTIONS
How do I setup the show page to show a user's evaluations received?
How do I adapt the form so that it specifies a user id as the person who should receive the evaluation?
How do I setup the show page to show a user's evaluations received?
Your model concerns should help you with that. In your UsersController#show action, simply adding the following should do the trick:
#received_evaluations = #user.received_evaluations
Then you can use it in your show template:
<% #received_evaluations.each do |evaluation| %>
// render some view stuff
<% end %>
Or use collection rendering.
note: the Evaluation.find(...) that's currently in your view should be put in the controller action, it's not good practice to leave that in the view.
How do I adapt the form so that it specifies a user id as the person who should receive the evaluation?
If you have identified the user that will serve as evaluatable you could set it in your controller action or in your view form in case you have a list of users to evaluate on your page.
In the controller:
#evaluation.evaluatable_id = user_to_evaluate.id
#evaluation.evaluatable_type = user_to_evaluate.class.to_s
Or this simpler statement should do the same:
#evaluation.evaluatable = user_to_evaluate
Similarly, you should be able to set the evaluator the same way:
#evaluation.evaluator = user_that_evaluates
In the view:
<% #users_to_evaluate.each do |user| %>
<%= simple_form_for(Evaluation.new) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %>
<%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %>
<%= f.hidden_field :evaluator_id, :value => current_user.id %>
<%= f.hidden_field :evaluator_type, :value => current_user.class.to_s %>
<%= f.hidden_field :evaluatable_id, :value => user.id %>
<%= f.hidden_field :evaluatable_type, :value => user.class.to_s %>
</div>
<% end %>
<% end %>

rails how get related id on view page

i'm trying to make this conditional:
<% if current_boutique.boutique_kind(3) %>
Brand: <span><%= current_boutique.name %></span>
<% else %>
<%= p.input :brand_id, :as => :select, :collection=> Brand.find(:all, :order=>:name).collect{ |b| [b.name,b.id, b.name]},
:label => "Marca", :prompt => 'Select Brand', :required => true %>
<% end %>
but the boutique_kind(3) show all boutiques! how i just get the boutiques with boutiques_kind id 3 method!
boutiqueKind Controller
def show
#boutique_kind = BoutiqueKind.find(params[:id])
end
boutiqueKind model
attr_accessible :kind, :slug
has_many :boutiques
has_many :products, :through => :boutiques
#belongs_to :gender
accepts_nested_attributes_for :boutiques
attr_accessible :boutiques, :boutiques_attributes, :kind
Boutique Controller
class Boutique < ActiveRecord::Base
belongs_to :user
belongs_to :boutique_kind
end
You should be able to find the boutique by ID with:
BoutiqueKind.find(3)
If I'm misunderstanding, and id is not a unique id (in which case I suggest you rename the column to something other than id), you can get all records where a certain field == a specific value with:
BoutiqueKind.where(id: 3)

validate_uniqueness_of scope not working

Currently I can create one item with multiple designs in unique picturelocs. Creating a second item gives me the following error that there already exists a pictureloc with that value, even though it is in a seperate item_id and shouldn't be worried over. This is my first question so cut me some slack on the formatting, and thanks in advance!
Error:
PG::Error: ERROR: duplicate key value violates unique constraint "index_designs_on_pictureloc"
DETAIL: Key (pictureloc)=(1) already exists.
: INSERT INTO "designs" ("created_at", "item_id", "picture_content_type", "picture_file_name", "picture_file_size", "picture_updated_at", "pictureloc", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"
Design.rb
class Design < ActiveRecord::Base
attr_accessible :picture, :pictureloc
has_attached_file :picture, styles: {medium: "150x150#"}
validates_attachment :picture, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png'], :message => 'must be a PNG, JPG, or JPEG'},
size: {less_than: 5.megabytes, :message => 'must be less than 5 megabytes'}
validates_uniqueness_of :pictureloc, scope: :item_id
belongs_to :item
def show_location
if pictureloc == 1
"Front"
elsif pictureloc == 2
"Back"
elsif pictureloc == 3
"Left Sleeve"
elsif pictureloc == 4
"Right Sleeve"
end
end
end
Item.rb
class Item < ActiveRecord::Base
attr_accessible :price, :make, :amount, :color, :note, :designs_attributes
validates_presence_of :make, :amount, :color
validates :amount, :numericality => { :greater_than => 0 }
belongs_to :quote
has_many :designs, :dependent => :destroy
accepts_nested_attributes_for :designs
end
Index in the schema:
add_index "designs", ["pictureloc"], :name => "index_designs_on_pictureloc", :unique => true
nested item view:
<!-- item form -->
<%= f.input :make, collection: #types, label: 'Thread Type' %>
<%= f.input :amount, label: 'How Many' %>
<%= f.input :color, collection: #colors %>
<!-- nested form for creating a design of an item -->
<%= f.simple_fields_for :designs, :html => { :multipart => true } do |designform| %>
<%= render "customdesign", g: designform %>
<% end %>
<!-- add/remove another design -->
<%= f.link_to_add "Add Design", :designs %>
<%= f.input :note, :input_html => { :cols => 50, :rows => 3 }, label: 'Special Notes or Requests' %>
<%= f.link_to_remove "Remove" %>
Nested design view:
<!-- upload/remove image form -->
<%= g.select( :pictureloc, { "Front" => 1, "Back" => 2, "Left Sleeve" => 3, "Right Sleeve" => 4 } ) %>
<%= g.file_field :picture %>
<%= g.link_to_remove "Remove" %>
Note that pictureloc is an integer, and that the designs get saved at the same time which is why I had to create an index (I think?)
I tried removing the unique: true from the index because maybe it was overkill, but that didn't solve anything.
Your index should be
add_index "designs", ["pictureloc", "item_id"], :name => "index_designs_on_pictureloc", :unique => tru

Rails nested form with has_many :through, not saving the data to joining table

I am kinda new to Rails and this is my first post to StackOverflow.
Say I have 3 models:
class Product < ActiveRecord::Base
default_scope :order => :title
has_many :line_items
has_many :promo_products
has_many :promotions, :through => :promo_products, :foreign_key => :promotion_id
before_destroy :ensure_not_referenced_by_any_line_item
before_destroy :ensure_not_referenced_by_any_promo_product
validates :title, :presence => true, :uniqueness => true
validates :description, :presence => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
def ensure_not_referenced_by_any_promo_product
if promo_products.empty?
return true
else
errors.add(:base, 'Some promotions are still in effect')
return false
end
end
end
class Promotion < ActiveRecord::Base
CART_OR_PRODUCT = ['Cart', 'Product']
PROMOTION_TYPE = ['Percentage based', 'Value based']
has_many :promo_products
accepts_nested_attributes_for :promo_products
has_many :products, :through => :promo_products, :foreign_key => :product_id
accepts_nested_attributes_for :products
#attr_accessible :promo_products_attributes, :title, :description, :cart_or_product, :promotion_type, :discount, :minimum_price, :minimum_quantity
validates :title, :description, :presence => true
validates :cart_or_product, :inclusion => {:in => CART_OR_PRODUCT, :message =>
"is invlaid. Please select a valid option"}
validates :promotion_type, :inclusion => {:in => PROMOTION_TYPE, :message =>
"is invalid. Please select a valid option"}
validates :discount, :minimum_price, :numericality => {:greater_than_or_equal_to => 0.00}
validates :minimum_quantity, :numericality => {:greater_than_or_equal_to => 0}
end
class PromoProduct < ActiveRecord::Base
belongs_to :promotion
belongs_to :product
accepts_nested_attributes_for :products
end
In the promotions new page, I would like to show list of products that could be part of a promotion. A user may select 0, 1 or more products, depending on the type of promotion.
In the action new of promotions_controller, I built like this:
#promotion.promo_products.build.build_product
In the _form of promotions, I needed to show the list of products for user to select. I made a nested form like:
<%= form_for(#promotion) do |f| %>
<!-- other promotion fields -->
<%= f.fields_for :promo_products do |pp| %>
<%= pp.fields_for :products do |p| %>
<div class="field">
<%= f.label "Products" %><br />
<%= collection_select :promo_product, :product_id, Product.all, :id, :title {:selected => #promotion.product_ids}, {:multiple => true} %>
</div>
<% end %>
<% end %>
<% end %>
I have 2 issues.
First my code throws an error:
ArgumentError in PromotionsController#new
No association found for name `products'. Has it been defined yet?
If I change the line in PromoProduct model:
accepts_nested_attributes_for :products
to
accepts_nested_attributes_for :product
Then there are no errors, and everything works fine.
The data doesn't get saved to promo_product table. I have the create action in promo_product controller as:
def create
#promotion = current_promotion
products = Product.select(:id => params[:product_id])
products.each do |p|
promo_product = #promotion.promo_products.build(p)
promo_product.save
end
##promo_product = PromoProduct.new(params[:promo_product])
redirect_to promotions_path
end
How can I go about it?
Thank you.
You shouldn't put the "accept_nested_attribute_for" in the association table PromoProducts. It should exist in the model that you want to use for creating association to another model. "accept_nested_attribute_for" IIRC simply inserts an "[association]_attributes=" method for your model. For instance, if you add this method to your Product class for Promotion, you will get "promotion_attributes=" method inserted in the Product class. Then a nested form can use this function to create new objects with a hash that represents the model and association.
Base on the above, the create action shouldn't be in PromoProduct controller, instead it should be in Promotion controller.
<%= form_for(#promotion) do |f| %>
<!-- other promotion fields -->
<%= f.fields_for :products do |pp| %>
<div class="field">
<%= f.label "Products" %><br />
<%= collection_select :promo_product, :product_id, Product.all, :id, :title {:selected => #promotion.product_ids}, {:multiple => true} %>
</div>
<% end %>
<% end %>
I don't know without trying if the above collection_select line is correct. But you can debug this by checking the parameter returned by the form to the controller in the server console log. Basically you should see a nested hash of
{:promotion => {:products => ...}}
Let me know if you need more help on this. In my solution I used a combination of select_tag and options_from_collection_for_select. (But I don't recall the behavior of all these offhand without looking at the API doc.)
Lastly, do you need the :through model? I think since you created the through model you need to handle saving that in your create action. But since you don't have other attributes on the PromoProducts table I wonder if you want to simply leave it as a HABTM association and let rails deal with the rest?

Resources