How can I achieve a nested rails form? Im having trouble getting this setup properly. Right now I have:
<%= simple_form_for #user do |f| %>
<%= f.input :city %>
<%= f.input :address %>
<%= f.input :zipcode %>
<%= f.association :interests, :as => :check_boxes, :label => false %>
<%= f.association :holidays, :as => :check_boxes, :label => false %>
<%= f.simple_fields_for :friend_birthdays do |friend_birthday| %>
<%= f.input :name %>
<%= f.input :gender, :collection => ['male','female'] %>
<% end %>
<%= f.button :submit %>
<% end %>
f.association is working fine for my interests & holidays models as they only need to collect a single attribute each. However, the friend_birthdays model has the same exact relationship as (interests & holidays) with the user model but requires multiple attributes to be edited/added. Any ideas?
If you are using Rails 3+, then it handles nested forms without any additional gems. The key is in using the "accepts_nested_attributes_for" method on the associations in your model, and the fields_for method on the form helper. Read up on them here and here.
I've never used simple_form, but I believe it drove the nested form development in Rails. So, taking a guess, you need to write your nested form references as:
<%= f.simple_fields_for :friend_birthdays do |friend_birthday| %>
<%= friend_birthday.input :name %>
<%= friend_birthday.input :gender, :collection => ['male','female'] %>
<% end %>
The point being, you need to call the helpers on the nested form, not the parent form.
Related
I am trying to make an app in Rails 4.
I use simple form for forms.
I have an industry model.
The industry.rb has:
scope :alphabetically, -> { order("sector ASC") }
The industry controller has:
def index
##industries = Industry.all
#industries = Industry.alphabetically
end
The industry form has:
<%= simple_form_for(#industry) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.select :sector, options_from_collection_for_select(Industry.alphabetical), :prompt => 'Select' %>
<%= f.input :icon, as: :file, :label => "Add an icon" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Submit", :class => 'formsubmit' %>
</div>
<% end %>
I'm trying to get my form input for :sector to use the collection of industries (by calling the scope).
When I try this, I get the following error:
undefined method `alphabetical' for #<Class:0x007fef65635220>
Can anyone see what's wrong?
It should be alphabetically instead of alphabetical.
Also, according to the options_from_collection_for_select documentation, you need to pass at least 3 arguments to the options_from_collection_for_select method: collection, value_method and text_method.
Change to the following to make it work:
<%= f.select :sector, options_from_collection_for_select(Industry.alphabetically, 'id', 'sector'), :prompt => 'Select' %>
I am having some difficulty to figure out why the post data from the form is not posted correctly.
I have to models: Child and Parent
in the form of Child i am nesting a form of Parent in this way:
<%
parent = (child.parent) ? parent : Parent.new
%>
<%=f.fields_for :parent, parent do |builder| %>
<%= render 'parent_fields', :fp => builder %>
<% end %>
The parent_fields form is as follows:
<% #all_parents = Parent.all %>
<% parent = fp.object %>
<%= fp.fields_for :parent do |builder| %>
<%= builder.input :parent_id, :as => :select, :label => 'Parent: ', :required => false,
:collection => options_from_collection_for_select(#all_parents, "id", "name", parent.id), :include_blank => '- Select -' %>
<% end %>
The posted data hash shows as follows:
"parent_attributes"=>{"parent"=>{"parent_id"=>"6"}, "id"=>"36"}
where 36 is the old parent id and 6 is the new one.
When i do update_attributes it does not work which is normal because it would work if the hash would be like this way:
...
"parent_id" => 6
"parent_attributes"=>{"id"=>"36", ....}
...
I am working on a legacy code. It is also possible that data was modified by javascript. The purpose of this post is to make sure that the way I am writing the form is the right way because I am new to nested forms.
Thank you
There are two possibly scenarios you want to consider:
1. A child needs to select an existing parent it belongs to.
2. A child needs to create a brand new parent that it belongs to.
It appears that you want to do #1, select an existing parent. If so, you do not need fields_for. Fields for is for creating new relations.
I'll show you some example code from an application I'm working on about schools, where a student belongs_to a grade level.
app/views/students/_form.html.erb
<%= form_for #student do |student_form| %>
<%= student_form.text_field :first %>
<%= student_form.text_field :last %>
<%= student_form.collection_select :grade_level_id, GradeLevel.all, :id, :name %>
<%= student_form.submit "Save" %>
<% end %>
Now, using your models (Child and Parent):
app/views/children/_form.html.erb
<%= form_for #child do |form| %>
<%= form.text_field :child_name %>
<%= form.number_field :age %>
<%= form.collection_select :parent_id, Parent.all, :id, :name %>
<%= form.submit "Save" %>
<% end %>
Note that I just put Parent.all straight into the collection_select. Creating #all_parents above isn't necessary.
EDIT
If you want to create new parents every time...
app/views/children/_form.html.erb
<%= form_for #child do |form| %>
<%= form.text_field :child_name %>
<%= form.number_field :age %>
<% form.fields_for :parent, #child.parent do |parent_fields| %>
<%= parent_fields.text_field :parent_name %>
<%= form.submit "Save" %>
<% end %>
Is there any way to render forms fragments with formtastic?
I have some checkboxes in my form which come from a one-to-many relationship.
So let's suppose a User form with a one-to-many Roles relationships. My formtastic form would look like
<%= semantic_form_for #user] do |f| %>
<%= f.inputs "Details" do %>
<%= f.input :name %>
<%= f.input :lastname %>
<% end %>
<%= f.inputs "Roles" do %>
<%= f.input :roles, :as => 'checkbox' %>
<% end %>
<% end %>
Now the line:
<%= f.input :roles, :as => 'checkbox' %>
Will output checkboxes for one-to-many. This works fine. But now I need to achieve some ActionController that outputs just the checkboxes... with no form and fieldset around. So that I can use it in my Ajax calls and update the "Roles" fieldset with fresh rendered checkboxes dinamically.
Any ideas? I'm stuck and can't figure out how to solve this.
I don't know if anyone had this need... anyway since nobody replied I found a solution by my self.
To render form fragments in the view (or partial view) file:
<% ref = nil %>
<% semantic_form_for [:admin, #user] do |f| %>
<% ref = f %>
<% end %>
<%= ref.input :roles, :as => :check_boxes, :required => false %>
Notice that only the last line actually outputs something, previous lines don't.
Experienced Rubyst or maybe even newbies may laugh at this... but I'm a noob(Ruby) and this is the best I could come up with. If any of you can tell a better way to do it I will be more than happy to learn.
Want to do a checkboxes for has_many :through. Railscast in 2007 recommends this: check_box_tag "product[category_ids][]", category.id, #product.categories.include?(category). Is this still relevant or is there a more natural way using form_for to do this in rails 3?
My recommendation is to check out Justin French's Formtastic gem: https://github.com/justinfrench/formtastic
It makes working with forms in rails really easy and intuitive.
Your form might look like this:
<%= semantic_form_for #product do |f| %>
<%= f.inputs do |f| %>
<%= f.input :name %>
<%= f.input :categories, :as => :check_boxes, :collection => Categories.all %>
<% end %>
<%= f.buttons %>
<% end %>
Much simpler then looping through and using the check box tag.
Once you go formtastic you never go back.
I'm a bit stuck on a 'has_one' and 'belongs_to' relationship and getting it to properly display in Formtastic. I have a person model that has one picture (a profile picture). I want the user to be able to select the picture using radio buttons. So far, I have:
<% form.inputs do %>
<%= form.input :picture, :as => :radio, :collection => #pictures %>
<% end %>
However, this fails (because the foreign key is stored on the 'belongs_to' side of associations in Rails. Any suggestions?
Ended up using custom controller code to fix. Use a variety of filters, etc.
Came across this in the "related" sidebar. I think this is a good use case for nested attributes -- from the Formtastic README:
Nested forms are also supported (don’t forget your models need to be setup correctly
with accepts_nested_attributes_for). You can do it in the Rails way:
<%= semantic_form_for #post do |form| %>
<%= form.inputs :title, :body, :created_at %>
<%= form.semantic_fields_for :author do |author| %>
<%= author.inputs :first_name, :last_name, :name => "Author" %>
<% end %>
<%= form.buttons %>
<% end %>
Or the Formtastic way with the :for option:
<%= semantic_form_for #post do |form| %>
<%= form.inputs :title, :body, :created_at %>
<%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
<%= form.buttons %>
<% end %>