I have two sections in a form, and I have a button that toggles their visibility. Is there a way to restrict the submit button from sending parameters from the hidden one? It's unfortunately creating courses without names or numbers, and it is not selecting an existing course if I use the collection_select.
projects/new.html.haml
= form_for [#user, #project] do |f|
# This part of the form is mostly shown to the user, but is failing to work correctly
= f.collection_select :course_id, #courses, :id, :name, { prompt: true }
# This part of the form is typically hidden, javascript reveals it.
.hidden
= f.fields_for :course do |builder|
= builder.text_field :name, class: 'large', placeholder: 'Ex: Calculus I'
= builder.label :number, 'Number'
= builder.text_field :number, class: 'new_project_course_number', placeholder: 'Ex: MATH-101'
= builder.hidden_field :user_id, value: current_user.id
project.rb
belongs_to :user
belongs_to :course
attr_accessible :course_id, :course_attributes
accepts_nested_attributes_for :course
course.rb
belongs_to :user
has_many :projects
user.rb
has_many :projects
has_many :courses
Please let me know if I am leaving off any vital information accidentally.
I think you might be looking for reject_if param for your nested attribute set.
For example:
accepts_nested_attributes_for : course, :reject_if => proc { |attributes|
attributes['name'].blank?
}
Or something like this. It allows you leave submitted form as is it, but only created nested course object, when name is preset (in your case, there is some placeholder, so you might use another check here)
Related
Example models:
class Person
has_many :auto_mappings, dependent: :destroy
class Vehicle
has_many :auto_mappings
class AutoMapping
belongs_to :person
belongs_to :vehicle
validates :person,
:vehicle,
:usage,
presence: true
In my ActiveAdmin form for Create/Edit on Person, I include an input f.has_many for the nested AutoMapping resource to add a single AutoMapping at a time, which works great. What I would like to accomplish is to somehow add a button that would add an AutoMapping for each Vehicle to the Person.
Is there an approach to accomplish this or is there another direction I should go?
Example AA form for Person:
form do |f|
f.inputs 'Details' do
f.input :name
f.input :age
f.has_many :auto_mappings, class: 'auto_mapping' do |item|
item.input :vehicle, include_blank: false, input_html: {class: "vehicle", disabled: item.object.persisted?}
item.input :usage,
as: :select,
collection: %w[drive ride both]
include_blank: false
end
end
# Maybe a custom member action to "Create Person with all Auto Mappings"?
# I would prefer to auto populate the form page with `AutoMappings` rather than create them all on Submit if possible.
f.actions
end
Thanks!
You would have to extend has_many.es6 to make an Ajax call and add the items.
I have this:
<%= f.collection_select :category_ids, Category.all, :id, :name, {} %>
inside my form and it creates the record no problem, however I'm lost on how to make more two or more records inside the same form, all the things I try either just create the one record or none at all. I found this solution, which I analyzed and was sure was gonna work (altough I don't really get on why it uses nil):
<%= f.fields_for :category_ids do |category| %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_1}) %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_2}) %>
<% end %>
but this time it creates no record at all.
Here are my models:
class Event < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
end
class Category < ApplicationRecord
has_many :categorizations
has_many :events, through: :categorizations
end
class Categorization < ApplicationRecord
belongs_to :event
belongs_to :category
end
You want to assign more than one Category to your Model? Post your model so we can figure what's going wrong when storing it.
Adding multiple: true (and size: 5) will expand your select field and by holding ctrl/cmd you can select multiple entries.
<%= f.collection_select :category_ids, Category.all, :id, :name, {multiple: true, size: 5} %>
I'm creating an admin interface where the admin (of a company) can add custom fields to their employees.
Example:
Models:
Employee: Basic info like name, contact info, etc (has_many employee_field_values)
EmployeeFields: These are the dynamic ones the admin can add (every company has different needs, it could be anything), lets say favorite_food
EmployeeFieldValues: The actual values based on the fields above, say pizza (belongs_to both models above)
What's a smart way of adding the EmployeeFieldValues fields while editing an employee?
I'm trying something simple like this, but not sure if I like it
# Controller
#custom_fields = EmployeeFields.all
# View
<%= form_for(#employee) do |f| %>
<%= f.text_field :first_name %>
<% #custom_fields.each do |custom_field| %>
<%= custom_field.name %>
<%= text_field_tag "employee_field_values[#{custom_field.name}]" %>
<% end %>
<%= f.submit :save %>
<% end %>
And then when updating, params[:employee_field_values] gives this:
<ActionController::Parameters {"favorite_food"=>"pizza"}>
So, not sure if this is a good direction, also I'm not sure how to handle future edits to an employee's custom_fields if they change.
I think it will be better to use EmployeeField as nested model and EmployeeFieldValue for select field.
For example:
Models
class Employee < ActiveRecord::Base
validates :name, presence: true
has_many :employee_field_values
accepts_nested_attributes_for :employee_field_values, reject_if: ->(x) { x[:value].blank? }
end
class EmployeeFieldValue < ActiveRecord::Base
belongs_to :employee
belongs_to :employee_field
end
class EmployeeField < ActiveRecord::Base
has_many :employee_field_values, inverse_of: :employee_field, dependent: :destroy
validates :title, presence: true, uniqueness: true
end
Controller
class EmployeesController < ApplicationController
def new
#employee = Employee.new
#employee.employee_field_values.build
end
end
View
= simple_form_for #employee, url: '/' do |f|
= f.input :name
= f.simple_fields_for :employee_field_values do |ff|
= ff.input :value
= ff.input :employee_field_id, collection: EmployeeField.all.map{|x| [x.title, x.id]}
Also you need to make buttons for adding/removing :employee_field_value, and you can do it with gem cocoon for example
OR you can build all objects in controller(for each EmployeeField) and do without select box
I have a simple app, which has three models Assessment, Question and AssessmentQuestion
In Assessment i have the association like,
class Assessment < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :questions, through: :assessment_questions
end
In Question i have,
class Question < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :bank_questions, dependent: :destroy
end
In AssessmentQuestion i have,
class AssessmentQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :assessment
end
assessment_questions table has :assessment_id, :question_id and :mark columns
I have admin interface using ActiveAdmin gem.
While creating assessments in admin interface, In admin/assessment.rb i have a form generated by the formtastic gem,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input :questions, as: :check_boxes, member_label: :id
f.input :creator_id
end
f.actions :commit
end
This looks fine and no problem. The thing that i want is while choosing questions through the checkboxes, i want a textbox below or side to each checkbox containing mark of the questions will be filled in text boxes respectively ( through question.assessment_question.mark association ), so that i can edit the prefilled marks of the questions while creating assessment or leave it as it is.
I have tried, but got some error like
undefined method `to_sym' for {:for=>:questions}:Hash
My code,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input for: :questions do | question |
f.input :question, as: :select
f.input question.assessment_question.mark
end
f.input :creator_id
end
f.actions :commit
end
Any solutions?
Finally, i figured it out.
I actually created a custom semantic_form_for form _form.html.erb as a partial and included this in admin/assessment.rb file
form partial: 'assessment/form'
That's how i solved.
Reference: http://activeadmin.info/docs/5-forms.html
I would make a custom input that contains a checkbox and a string input:
https://github.com/justinfrench/formtastic#modified--custom-inputs
The solution is obvious, your "f.input :for" misses an "s".
It should be f.inputs
I'm using Simple Form and don't how you would show the values of 2 associations.
A Price can belong to a service or product but not both at the same time.
Price
# service_id, product_id
belongs_to :services # service.name
belongs_to :products # product.name
end
Instead having my simple form look like this:
<%= f.association :product, :input_html => { :class => "span5 } %>
<%= f.association :service, :input_html => { :class => "span5 } %>
I want to turn it into one field instead.
What is the way with simple_form_for?
What about with a regular form_for?
I think better way to do is to use polymorphic association.
class Price
belongs_to :pricable, polymorphic: true
end
class Product
has_one :price, as: :priceable
end
class Service
has_one :price, as: :priceable
end
Then, in your form, you can use:
<%= form_for [#priceable, Price.new]
where #priceable is a product or a service.