I have created a table name tenants which have following column
class CreateTenants < ActiveRecord::Migration
def change
create_table :tenants do |t|
t.text :company_name
t.text :work_area
t.text :second_pref
t.text :third_pref
t.integer :who_are_you
t.integer :number_of_bedroom
t.text :other_specs
t.string :budget
t.string :name
t.string :email
t.string :contact_number
t.timestamps null: false
end
end
end
And i am entering the data into in table by following form
<%= form_for Tenant.new do |val| %>
<%= val.label :company_name, "Company Name" %>
<%= val.text_field :company_name, class: 'form-control' %>
<%= val.label :work_area, "Work Area" %>
<%= val.text_field :work_area, class: 'form-control' %>
<%= val.label :second_pref, "Second Preference" %>
<%= val.text_field :second_pref, class: 'form-control' %>
<%= val.label :third_pref, "Third Preference" %>
<%= val.text_field :third_pref, class: 'form-control' %>
<%= val.label :who_are_you, "Are you Family/Bachelor?" %>
<%= val.text_field :who_are_you, class: 'form-control' %>
<%= val.label :number_of_bedroom, "Number of Bedroom" %>
<%= val.text_field :number_of_bedroom, class: 'form-control' %>
<%= val.label :other_specs, "Other Requirments" %>
<%= val.text_field :other_specs, class: 'form-control' %>
<%= val.label :budget, "Your Budget" %>
<%= val.text_field :budget, class: 'form-control' %>
<%= val.label :name, "Name" %>
<%= val.text_field :name, class: 'form-control' %>
<%= val.label :email, "Email" %>
<%= val.text_field :email, class: 'form-control' %>
<%= val.label :contact_number, "Contact Number" %>
<%= val.text_field :contact_number, class: 'form-control' %>
<%= val.submit "Submit", class: "btn btn-primary" %>
<% end %>
When i fill all the required filled and click submit, i see the following output in rails server log.
Started POST "/tenants" for ::1 at 2015-11-15 11:41:27 +0530
Processing by TenantsController#create as HTML
Parameters: {
"utf8"=>"✓", "authenticity_token"=>"26KYMFmofF+A1UrF+eWu21nEGbVO3n2bUSPl8340k8hY1JQhYF2kfhOHLmlF+r1Tj5UB7h6H+IJ7MY+Rx+o4CA==",
"tenant"=>
{
"company_name"=>"Housing.com",
"work_area"=>"Hiranandani Business Park",
"second_pref"=>"Chandivali",
"third_pref"=>"Vikhroli",
"who_are_you"=>"Bachelor",
"number_of_bedroom"=>"3",
"other_specs"=>"Gym, Swimming Pool",
"budget"=>"55000",
"name"=>"Shravan Kumar Gond",
"email"=>"shravan.ma.iitkgp#gmail.com",
"contact_number"=>"9475593772"
},
"commit"=>"Submit"
}
Unpermitted parameter: budget
(0.1ms) begin transaction
(0.1ms) rollback transaction
Can anyone tell me, why this happening ?
It seems you are missing :budget in strong parameters of your tenants_controller.rb file. It should be something like this.
private
def tenant_params
params.require(:tenant).permit(:company_name,
:work_area,
:second_pref,
:third_pref,
:who_are_you,
:number_of_bedroom,
:other_specs,
:budget,
:name,
:email,
:contact_number)
end
Unpermitted parameter: budget
This is your error - it means you're passing parameters to your controller, but it cannot save them in your model.
The fix is to set the strong params method (as shravan40 suggested).
Since you're calling Tenant.new in your #new action, I would recommend using the following in your controller:
#app/controllers/tenants_controller.rb
class TenantsController < ApplicationController
def new
#tenant = Tenant.new
end
def create
#tenant = Tenant.new tenant_params
#tenant.save
end
private
def tenant_params
params.require(:tenant).permit(:company_name, :work_area, :second_pref, :third_pref, :who_are_you, :number_of_bedroom, :other_specs, :budget, :name, :email, :contact_number)
end
end
There's also something you can do to polish up your form...
<%= form_for #tenant do |val| %>
<% vals = [[:company_name],[:work_area],[:second_pref, "Second Preference"], [:third_pref, "Third Preference"],[:who_are_you, "Are you Family/Bachelor?"], [:number_of_bedroom], [:other_specs, "Other Requirements"],[:budget, "Your Budget"], [:name], [:email], [:contact_number]] %>
<% vals.each do |value| %>
<% value[1] ||= value[0].to_s.gsub("_", " ") %>
<%= val.label value[0], value[1] %>
<%= val.text_field value[0], class: 'form-control' %>
<% end %>
<%= val.submit "Submit", class: "btn btn-primary" %>
<% end %>
Related
I am using a PostgreSQL database and Rails 5.0.6.
I try to build a course allocation WebApp for the school where I am working. For each course the teachers are able to select which forms are allowed to visit the course.
Migration file:
def up
create_table :courses do |t|
t.integer :number, null: false
t.string :name, null: false
t.text :description, null: false
t.decimal :level, array: true, default: []
t.integer :max_visitor, null: false
t.integer :min_visitor
t.integer :pos_visit
t.timestamps
end
end
In my Controller:
params.require(:course).permit(:number, :name, :description, :level [], :max_visitor, :min_visitor, :pos_visits)
I already read this post: Rails 5 strong params with an Array within check boxes values. But the Syntax params.require(:product).permit(:id, **category_ids: []**) doesnt work for me even though I am using rails 5 as well. I am not sure if :level [] really works but it seems to be correct syntax.
This is my Form:
<%= form_for #course do |t| %>
<%= t.text_field :number, class: 'form-control' %>
<%= t.text_field :name, class: 'form-control' %>
<%= t.text_area :description, class: 'form-control' %>
<%= t.check_box :level[], 1%>
<%= t.check_box :level[], 2%>
<%= t.check_box :level[], 3%>
<%= t.check_box :level[], 4%>
<%= t.text_field :max_visitor, class: 'form-control' %>
<%= t.text_field :min_visitor, class: 'form-control' %>
<%= t.text_field :pos_visit, class: 'form-control' %><br/>
<%= t.submit "bestätigen", class: "btn btn-success"%>
<% end %>
This check_box syntax seems to be wrong.
May anyone help me with the right syntax of the check_boxes?
Thanks for help.
There's a collection_check_boxes helper method for this:
<%= form_for #course do |f| %>
<%= f.collection_check_boxes(:level, { 'One': 1, 'Two': 2, 'Three': 3 }, :last, :first) %>
<% end %>
The third argument is the method used to get the value from the "collection", and the fourth is the method used to get the label from the "collection". This helper method automatically converts the Hash into an array, that's why I'm using last and first here.
It's also possible to style it the way you want e.g. using Bootstrap:
<%= f.collection_check_boxes(:level, { 'One': 1, 'Two': 2, 'Three': 3 }, :last, :first) do |b| %>
<div class="form-check form-check-inline">
<%= b.check_box class: 'form-check-input' %>
<%= b.label class: 'form-check-label' %>
</div>
<% end %>
<%= check_box_tag 'level[]', 1%>
<%= check_box_tag 'level[]', 2%>
<%= check_box_tag 'level[]', 3%>
<%= check_box_tag 'level[]', 4%>
But when you use check_box_tags in form_for, then the parameters level[], will be outside off the strong parameters array you usually use in the controller#new function.
Parameters: {"course"=>{"number"=>"12", "name"=>"tanzen", "description"=>"efwefggw", "max_visitor"=>"12", "min_visitor"=>"5", "pos_visit"=>"2"}, "level"=>["1", "3", "4"], "commit"=>"bestätigen"}
So I added the level manually
#course = Course.new(course_params)
#course.level = params[:level]
I am running a Rails 5.1 app with the following information:
Models
class Company < ApplicationRecord
has_many :complaints
accepts_nested_attributes_for :complaints
validates :name, presence: true
end
class Complaint < ApplicationRecord
belongs_to :company
validates :username, :priority, presence: true
end
Controller
class ComplaintController < ApplicationController
def new
#company = Company.new
#company.complaints.build
end
def create
#company = Company.new(company_params)
respond_to do |format|
if #company.save
format.html { redirect_to complaint_url }
else
format.html { render :new }
end
end
end
private
def company_params
params.require(:company).permit(:name, complaints_attributes: [:username, :priority])
end
Form in view
<%= form_for #company do |f| %>
<%= f.label :name, "Company" %>
<%= f.text_field :name, type: "text" %>
<%= f.fields_for :complaints do |complaint| %>
<%= complaint.label :username, "Username" %>
<%= complaint.text_field :username %>
<%= complaint.label :priority, "Priority" %>
<%= complaint.text_field :priority %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
If I have just one input field for the complaint_attributes part of the form (in other words just one field for username and one field for priority as shown above), this works just fine.
However, if I want to have multiple fields for username/priority in the form, so that I can submit multiple username/priority combinations in a single submission, I find that submitting the form will only save the last username/priority values from the form. Example of this view would be:
<%= form_for #company do |f| %>
<%= f.label :name, "Company" %>
<%= f.text_field :name, type: "text" %>
<%= f.fields_for :complaints do |complaint| %>
<div>
<%= complaint.label :username, "Username" %>
<%= complaint.text_field :username %>
<%= complaint.label :priority, "Priority" %>
<%= complaint.text_field :priority %>
</div>
<div>
<%= complaint.label :username, "Username" %>
<%= complaint.text_field :username %>
<%= complaint.label :priority, "Priority" %>
<%= complaint.text_field :priority %>
</div>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
I noticed that when submitting the form, I get a hash like this (for submitting single complaint):
{"utf8"=>"✓", "authenticity_token"=>"...", "company"=>{"name"=>"Test", "complaints_attributes"=>{"0"=>{"username"=>"test_person", "priority"=>"1"}}}, "commit"=>"Submit"}
Is there any way to modify the params to make it similar to this and have it saved to the DB?:
{"utf8"=>"✓", "authenticity_token"=>"...", "company"=>{"name"=>"Test", "complaints_attributes"=>{"0"=>{"username"=>"test_person", "priority"=>"1"}"1"=>{"username"=>"test_person", "priority"=>"2"}}}, "commit"=>"Submit"}
Or if not the above, what would be the best way to have the username/priority values saved if using multiple fields for them in a single form?
EDIT: I should point out that I can dynamically add the username/priority field groups as needed, so I don't want to be restricted to a set number.
the second block will override the first fields... you should instead build many complaints in the controller:
def new
#company = Company.new
3.times { #company.complaints.build }
end
and then with the following form it should generate to inputs according to the number of complaints you have built:
<%= form_for #company do |f| %>
<%= f.label :name, "Company" %>
<%= f.text_field :name, type: "text" %>
<%= f.fields_for :complaints do |complaint| %>
<%= complaint.label :username, "Username" %>
<%= complaint.text_field :username %>
<%= complaint.label :priority, "Priority" %>
<%= complaint.text_field :priority %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
I have created a valuation table which contain
class CreateValuations < ActiveRecord::Migration
def change
create_table :valuations do |t|
t.text :location
t.integer :number_of_bedroom
t.integer :number_of_bath
t.integer :age_of_building
t.text :other_details
t.string :name
t.string :email
t.integer :contact_number
t.timestamps null: false
end
end
end
And later i added a column :building of type text in it using rails generate migration add_building_to_valuations building:text. I have created a form
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#valuation) do |val| %>
<%= val.label :building, "Name of Building/Builder" %>
<%= val.text_field :building, class: 'form-control' %>
<%= val.label :location, "Location" %>
<%= val.text_field :location, class: 'form-control' %>
<%= val.label :number_of_bedroom, "Number of Bedroom" %>
<%= val.text_field :number_of_bedroom, class: 'form-control' %>
<%= val.label :number_of_washroom, "Number of Washroom" %>
<%= val.text_field :number_of_bath, class: 'form-control' %>
<%= val.label :age_of_building, "Age of Building" %>
<%= val.text_field :age_of_building, class: 'form-control' %>
<%= val.label :name, "Name" %>
<%= val.text_field :name, class: 'form-control' %>
<%= val.label :email, "Email" %>
<%= val.text_field :email, class: 'form-control' %>
<%= val.label :contact_number, "Contact Number" %>
<%= val.text_field :contact_number, class: 'form-control' %>
<%= val.submit "Evaluate my property", class: "btn btn-primary" %>
<% end %>
</div>
</div>
When i fill all the field and submit via Evaluate my property, Then no data is being saved in :building.
I checked this using following commands
$ rails console
$ Valuation.all
and received output are following output
#<Valuation id: 1, location: "Chandivali, Powai",
number_of_bedroom: 3, number_of_bath: 3,
age_of_building: 9, other_details: nil, name: "Shravan",
email: "shravan.xxx#gmail.com",
contact_number: "9876543210",created_at: "2015-11-14 09:47:26",
updated_at: "2015-11-14 09:47:26", building: nil>,
You should update your valuation_params Which is most likely a private function
private
def valuation_params
params.require(:valuation).permit(:building,
:location,
:number_of_bedroom,
:number_of_bath,
:age_of_building,
:name, :email, :contact_number)
end
end
I have polymorphic associations with different models that I want to save upon submitting the registration form using devise. i.e:
User
Company + ContactInfo
Employee + ContactInfo
I understand that nesting forms is not recommended but What would be the best way to achieve this?
Thanks
models:
class User < ActiveRecord::Base
belongs_to :company
accepts_nested_attributes_for :company
end
class Company < ActiveRecord::Base
has_many :employees
has_one :contact_info, as: :contactable
accepts_nested_attributes_for :contact_info
end
class Employee < ActiveRecord::Base
belongs_to :company
has_one :contact_info, as: :contactable
accepts_nested_attributes_for :contact_info
end
class ContactInfo < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
end
migrations:
class CreateCompanies < ActiveRecord::Migration
def change
create_table :companies do |t|
t.string :name
t.references :contact_info, index: true
t.string :website
t.timestamps null: false
end
add_foreign_key :companies, :contact_infos
end
end
class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees do |t|
t.string :first_name
t.string :last_name
t.references :company, index: true
t.references :contact_info, index: true
t.string :job_title
t.timestamps null: false
end
add_foreign_key :employees, :contact_infos
end
end
class CreateContactInfos < ActiveRecord::Migration
def change
create_table :contact_infos do |t|
t.string :email
t.string :phone
t.string :mobile
t.string :contactable_type
t.integer :contactable_id
t.timestamps null: false
end
end
end
registration controller:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up).push(:name, :email, :password, company_attributes: [ :id, :name, :website, :company_type, :number_of_employees, contact_info_attributes: [ :id, :email, :phone, :mobile]])
end
devise's new registration:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'plan', params[:plan] %>
<% resource.build_company %>
<%= f.fields_for :company do |f| %>
<%= render "companies/fields", f: f %>
<% end %>
<% resource.company.build_contact_info %>
<%= f.fields_for :contact_info do |f| %>
<%= render "contact_infos/fields", f: f %>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<%= f.submit 'Sign up', :class => 'button right' %>
<% end %>
Console:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Nb6Na8P93s1dYvlDIQuiG11IoDeSzSylH4BCN8Tm7ipxCsbsdiWjDx5tJpijwldkjK4pPfjuwROnEvybYS7UIQ==", "plan"=>"free", "user"=>{"company_attributes"=>{"name"=>"Company Name", "website"=>"company website", "company_type"=>"company type", "number_of_employees"=>"121"}, "contact_info"=>{"email"=>"company#email.com", "phone"=>"1234", "mobile"=>"1234"}, "name"=>"user_name", "email"=>"user_email#email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: contact_info
only User and company params are saving well. I was trying to get the devise_parameter_sanitizer to work with nesting contact_info in company first without trying the same with employee just yet, any idea what I'm doing wrong or any tips if im on the right track?
Thanks!
Update:
However contact_info params are permitted if I nest the contact_info form fields within the company form fields like so:
<% resource.build_company %>
<%= f.fields_for :company do |f| %>
<%= render "companies/fields", f: f %>
<% resource.company.build_contact_info %>
<%= f.fields_for :contact_info do |cf| %>
<%= render "contact_infos/fields", f: cf %>
<% end %>
<% end %>
the question is, is that good practice?
This is a guess - but your contact_info for the company isn't nested inside the company section. Try something like this (note, not tested, probably buggy)
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'plan', params[:plan] %>
<% resource.build_company %>
<%= f.fields_for :company do |f| %>
<%= render "companies/fields", f: f %>
<%# this is now nested inside the company-fields %>
<% resource.company.build_contact_info %>
<%= f.fields_for :contact_info do |f| %>
<%= render "contact_infos/fields", f: f %>
<% end %>
<% end %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<%= f.submit 'Sign up', :class => 'button right' %>
I've been reading this question (Ruby on Rails: Submitting an array in a form) but it didn't answer my question.
I have this form:
<div class="signup pull-right">
<div>
<p style="color: white">Create Doctor:</p>
<%= form_for(#doctor) do |d| %>
<%= d.text_field :name[1], :placeholder => "name" %>
<%= d.text_field :name[2], :placeholder => "surname" %>
<%= d.text_field :email, :placeholder => "email" %>
<%= d.text_field :password, :placeholder => "password" %>
<%= d.text_field :password_confirmation, :placeholder => "password confirmation" %>
<%= d.submit "Sumbit", class: "btn btn-small btn-primary pull-right", id: "boton" %>
<% end %>
</div>
where name is an array. What I want is to catch name and surname separately and join them inside name[]. I couldn't found the way to do it easily. Do you have any suggestion?
Thanks in advance.
You don't need such hack to solve the problem. Setting the attributes by virtual cattr_accessor would be simple and conventional.
class Doctor < ActiveRecord::Base
cattr_accessor :first_name, :surname
# others...
end
Then in form
<%= form_for(#doctor) do |d| %>
<%= d.text_field :first_name, :placeholder => "first name" %>
<%= d.text_field :surname, :placeholder => "surname" %>
Then in controller
def create
attr = params[:doctor]
attr[:name] = "#{attr.delete!(:first_name)} #{attr.delete!(:surname)}"
#doctor = Doctor.new(attr)
if #doctor.save
# blah blah
end
I would check till I have this html generated:
<input name='doctor[name][]'>
I would try:
<%= text_field_tag 'doctor[name][]' %>