Translate form helper to simple_form or formstatic - ruby-on-rails

I have this in my model:
class Person < ActiveRecord::Base
RELATIONSHIP_STATUSES = [
"single",
"in a relationship",
"together",
"it's complicated"
]
validates :relationship_status, :inclusion => RELATIONSHIP_STATUSES
end
This in the view:
collection_select(:person, :relationship_status, Person::RELATIONSHIP_STATUSES, :to_s)
And I would like to translate that to simple_form. Is that possible?

If I understood you right, it's simple(it's for formtastic):
<%= form.input :relationship_status, :as => :select, :collection => Person::RELATIONSHIP_STATUSES %>

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

belongs_to id not being set activeadmin

I have two models: Asssessment and Question which are organized like this:
class Question < ActiveRecord::Base
belongs_to :assessment
class Assessment < ActiveRecord::Base
has_many :questions
I'm trying to create an activeadmin (ver 1.0.0) interface to create assessments and add questions to them.
So far I've tried making a questions tab:
ActiveAdmin.register Question do
permit_params :question_text, :question_type, :scale_min, :scale_max
form do |f|
f.inputs "Question Information" do
f.input :assessment, :as => :select, :collection => Assessment.non_daily_assessments
f.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
f.input :question_text, :input_html => {:rows => 2, :cols => 10}
f.input :scale_min
f.input :scale_max
end
f.actions
end
non_daily_assessments simply returns a subset of all assessments
I am able to select from a list of assessments, but when I save the question and am taken to the "view question" page the question's assessment_id is empty.
Similarly, if I create an assessments tab:
ActiveAdmin.register Assessment do
permit_params :name, :questions
form do |f|
f.inputs "Assessment Information" do
f.input :name, :input_html => {:rows => 1, :cols => 10}
f.has_many :questions, :allow_destroy => true, :heading => 'Questions' do |qf|
qf.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
qf.input :question_text, :input_html => {:rows => 2, :cols => 10}
qf.input :scale_min
qf.input :scale_max
end
end
f.actions
end
I am able to go to a particular assessment and start adding questions, but when I reload the page they're gone. Going into the console I see that the questions were created, but their assessment_id's are nil just like through the question tab.
What is a correct way to create an activeadmin interface for a belongs_to has_many relationship?
Let me know if you need more information.
Your permit_params are incomplete. Have a look at this answer: Nested form in activeadmin not saving updates
You need to add :assessment_id to the permit_params in the Question section, and if you want to be able to edit questions with the assessments, you're probably missing the accepts_nested_attributes_for :questions in the Assessment model, and you'll also need to change the permit_params in the Assessment section to something like
permit_params :name, questions_attributes: [:id, :question_type, :question_text, :scale_min, :scale_max]

Rails has_many :through and collection_select with multiple

I have the following problem using a has_many :through many-to-many relation in a multi-select via collection_select :multiple => true. I have Suppliers who deliver many Ingredients which can be delivered by many Suppliers. Have a look:
The Ingredient model:
class Ingredient < ActiveRecord::Base
has_many :ingredient_suppliers
accepts_nested_attributes_for :ingredient_suppliers, :allow_destroy => true
has_many :suppliers, :through => :ingredient_suppliers
end
The Supplier model:
class Supplier < ActiveRecord::Base
has_many :ingredient_suppliers
has_many :ingredients, :through => :ingredient_suppliers
end
The relationship Entity:
class IngredientSupplier < ActiveRecord::Base
belongs_to :ingredient
belongs_to :supplier
end
And this is the form. Note that I could not get it to work without specifying the :name:
<%= form_for(#ingredient) do |f| %>
<%= f.fields_for :suppliers do |supplier_fields| %>
<%= supplier_fields.collection_select (:supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name,
{:selected => #ingredient.supplier_ids,
:include_blank => true},
{:multiple => true,
:name => 'ingredient[supplier_ids]'}) %>
<% end %>
<% end %>
If I remove the :name, then I get this error message:
Supplier(#-617951108) expected, got Array(#-608411888)
Request
Parameters:
{"commit"=>"Anlegen",
"authenticity_token"=>"MuEYtngwThharmM1KaAbH8JD3bScXiDwj0ALMytxl7U=",
"_method"=>"put",
"utf8"=>"✓",
"id"=>"1",
"ingredient"=>{"name"=>"Ingredient 1",
"nr"=>"00100",
"unit"=>"kg",
"mol_per_unit"=>"2000,
00000",
"description"=>"",
"suppliers"=>{"supplier_ids"=>["1",
"2"]}}}
The problem now is, that the PUT parameters only contain one supplier_id instead of an array of supplier_ids:
"ingredient"=>{"name"=>"Rohstoff 3", "nr"=>"00300", "unit"=>"Stk.", "mol_per_unit"=>"0,00000", "description"=>"", "supplier_ids"=>"2"}
I've got the problem solved. In this case, using fields_for was the error. The solution is using a collection_select, like this:
<%= collection_select(:ingredient, :supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name, {:selected => #ingredient.supplier_ids, :include_blank => true}, {:multiple => true}) %>

Rails nested model with formtastic is skipping one field?

I have this structure models
class Tournament < ActiveRecord::Base
AGES = ["5u", "6u", "7u", "8u"]
has_many :courts, :dependent => :destroy
accepts_nested_attributes_for :courts, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
class Court < ActiveRecord::Base
belongs_to :tournament, :autosave => true
has_many :ages, :dependent => :destroy
accepts_nested_attributes_for :ages, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
class Age < ActiveRecord::Base
belongs_to :court
Now my forms look like this
_form.html.erb
<%= semantic_form_for #tournament do |f| %>
<%= f.inputs do %>
<%= f.input :name, :hint => "What is the name of the Tournament?" %>
<%= f.semantic_fields_for :courts do |builder| %>
<%= render :partial => "court_fields", :locals => { :f => builder } %>
<% end %>
_court_fields.html.erb
<div class="nested_fields">
<%= f.input :name, :input_html => {:class => "name"} %>
<%= f.semantic_fields_for :ages do |builder| %>
<%= render :partial => "age_fields", :locals => { :f => builder } %>
<% end %>
_age_fields.html.erb
Testing ...am I getting in here
<%= f.input :name, :as => :check_boxes, :collection => Tournament::AGES, :input_html => {:class => "age_limits"} %>
everything seems to work well except nothing shows up in the ages_fields partial...not the checkboxes and not even the dummy text Testing ...am I getting in here is not displaying....any ideas what could be causing the issue
The obvious reason I can think of: are you sure your Court has ages ?
[EDIT] That the Court has the relation was indeed clear to me.
But your code will only show an age for a court if it already exists.
From your output in the comments: the court has no actual ages so no ages are shown.
If you do this in your controller:
def new
#tournament = Tournament.new
#tournament.courts.build
#tournament.courts[0].ages.build
end
This will make sure that you have at least one (empty) court and one (empty) age.
Otherwise you could also consider using a gem like cocoon to dynamically add new elements if needed.
Hope this helps.

How to Validate the value of a form in Rails 3

I'm trying to validate the value of a form (a checkbox actually) in a model, but am having a lot of trouble finding what to pass validates:
validates :agreement, :agreement => true
I've gotten other things to work like:
validates :password, :presence => true, :length => {:minimum => 6, :maximum => 25}, :confirmation => true
My view looks like this:
<% form_for :signup_form, :url => {:controller => "user", :action => "post_signup"} do |f| %>
...
<%= f.check_box( :agreement ) %> I agree to the <%= link_to("Terms of Service", :controller=> "about", :action => "terms") %> and <%= link_to("Privacy Policy", :controller=> "about", :action => "privacy") %>
...
Which then goes to my controller:
agreement = params[:signup_form][:agreement]
new_user = User.create(:login_name => login_name, :first_name => first_name, :last_name => last_name, :email => email, :password => password, :agreement => agreement, :created_at => DateTime.now())
And then my model.
Thanks for any help you can offer in advance.
You might be looking for :acceptance => true or validates_acceptance_of
You'll want to display your errors on the page, and ensure your validation is working at all. I would re-implement your validation as:
# app/models/user.rb
class User < ActiveRecord::Base
validates :agreement do |ag|
ag.errors.add "Must agree to the terms" unless self.agreement
end
end
see http://asciicasts.com/episodes/211-validations-in-rails-3 for a comprehensive treatment, including a nice way to display the errors.

Resources