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.
Related
I have a model Driver which has many relationship to claims, I wish to have a form that client can enter their detail, together with a number of claims they've had in the past.
In the model, I have:
has_many :claims
and in the form I entered:
<%= semantic_form_for #driver do |f| %>
<%= f.input :name %>
.......
<%= f.inputs :for => :claims do |c| %>
<% c.input :happen_date %>
<% c.input :claim_details %>
...........
<% end %>
<% end %>
When I submit the form, the issue happens, I got Claim(#3213231311) expected, got Array(#70299228017580)
And I had a look at the form, I saw:
{"utf8"=>"✓",
"authenticity_token"=>"VD3lt+LXZoA94YoL3PkI0frTH5EzT4vs/lZhzQhp0IQ=",
"driver"=>{
"name"=> "test tester",
.......
"claims"=>{"happend_date(3i)"=>"10",
"claim_date(2i)"=>"10",
"claim_date(1i)"=>"2012",
"claim_details"=>"dsadsadsadas"}},
"commit"=>"Next"}
Since there is a has_many relationship with the claims, shouldn't claims be covered in an array like:
"claims" => [{.....},{......}]
I've been working so hard on the form and still couldn't get claims to be send as array, anyone help?
Did you include this in your 'Driver' model:-
accepts_nested_attributes_for :claims
Edit your form like :
<%= semantic_form_for #driver do |f| %>
<%= f.input :name %>
<%= f.semantic_fields_for :claims do |c| %>
<% c.input :happen_date %>
<% c.input :claim_details %>
<% end %>
<% end %>
Hope it will help. Thanks
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.
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 have a nested form, like:
<% form_for #invoice do |f| %>
<%= render :partial => "invoice_item_fields", :locals => {:f => f} %>
<% end %>
and _invoice_items_fields:
<% f.fields_for :invoice_items do |builder| %>
<%= link_to_remove_fields "remove", builder %>
<%= builder.collection_select(:product_id, Product.all, :id, :name) %>
<%= builder.text_field :quantity, :size => 4,%>
<% end %>
When i submit the form and it not pass the validations it render the new action again. The thing is the selected value for :product_id is no remembered, but the :quantity is ok.
I read that i should setup an instance variable in the controller with the value of the selected option and then do something like:
<%= builder.collection_select(:product_id, Product.all, :id, :name, :selected => #selected_product) %>
but the thing is the application could have many :invoice_items, so i don't know what to do for the select field "remember" the values.
Thanks.
Can you post your controller code? I'd like to see where you're setting the invoice & invoice_item fields and how you're processing the form. I recently built an invoice & line_item application and might have some ideas... [Sorry I can't comment, but haven't hit that permission level.]
I solved this on another post. The thing was that my product_id was and string and not and integer at db level.
I'm trying to create a drop down menu to allow a user to change an entry's field in my table. The user has one of three options -- hot, medium and cold.
I already have text_fields that do essentially the same thing for other fields, that all update when the user clicks on a submit_tag.
Is there an easy way to implement a drop-down box and have the result saved with the submit_tag ?
thanks,
-Chris
Here's the basic answer. The array of two element arrays is the critical part.
<% form_for #entry do |f| %>
<%= f.text_field :name %>
<%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
<%= f.submit %>
<% end %>
I'll assume 2 things:
That you are the <%= form_for #model_instance idiom (explained on section 2.2 of this guide).
That you want to store the "hot", "medium" and "cold" values as strings (not as numbers 1,2 and 3 or something similar) on your database.
Let's say that you have two fields, called :name and :temperature, controlled by two text_fields:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.text_field :temperature %>
<%= f.submit "Create" %> <% end %>
<% end %>
Now you want to change the :temperature control to a dropdown list, accepting hot, medium and cold as values. Then you can do that this way:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.collection_select :temperature, Article::TEMPERATURES, :to_s, :to_s,
:include_blank => true
%>
<%= f.submit "Create" %> <% end %>
<% end %>
You will now have to define the Article::TEMPERATURES constant in your Article model. It shouldn't be very difficult:
class Article < Activerecord::Base
TEMPERATURES = ['hot', 'medium', 'cold']
You may be wondering why I added the :include_blank part on the collection_select. This will add an "empty" option on your dropdown list. You will need that empty option when creating new objects, unless you want a "default" value to temperature.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001730
I was working on something similar. I got this to work by simply adding either an enum or a constant(similar to what kikito said previously) in my model and then calling the select in my form.
Here's how it can work.
using the constant:
class ClassName < ActiveRecord::Base
TEMPERATURES = ['Hot', 'Medium', 'Cold']
end
bin/rails g migration add_column_to_table temperatures:string
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName::TEMPERATURE %>
or
using the enum:
class ClassName < ActiveRecord::Base
enum temperature: [:hot, :medium, :cold]
end
bin/rails g migration add_column_to_table temperatures:integer
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName.temperatures.keys %>
Hope that helps you!
You might want to consider formtastic gem which is lot less code.
<% semantic_form_for #stuff do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :temperature, :as => :select,
:label => "Degree", :include_blank => false,
:collection => [["Hot", 1], ["Medium", 2], ["Cold", 3]] %>
<% end %>
<%= f.buttons %>
<% end %>
In accordance with all of the above answers, remember to do this last, important step:
Restart your server!
As a newbie, I was wondering why my array was not working even though I followed all the steps correctly.