I am creating a virtual attribute in my model:
def entities
#entities = Array.new()
#entities.push(self.contact.name)
#entities.push(self.contact.partner.name) if self.contact.partner
#entities.push('Joint') if self.contact.partner
#entities
end
Then in my form I'm trying to use this array from a nested array. I'm using simple form so it looks like this
<%= f.input :ownership, collection: :entities, :include_blank => false, :label => false %>
However this gives me an error:
undefined method `to_a' for :entities:Symbol
If I have created an array, I don't understand why it isn't rendering as an array. What am I missing?
You can't use :entities as the collection:
<%= f.input :ownership, collection: :entities ...%>
That doesn't work. The error indicates that Simple Form is attempting to convert the argument :entities to an array, which is causing an error.
You need to give it an actual collection:
<%= f.input :ownership, collection: #object.entities ... %>
Related
I have the following code in my Rails 3 application, it's supposed to be displaying a select box with each asset_type record:
assets_helper
def asset_type_all_select_options
asset_type.all.map{ |asset_type| [asset_type.name, asset_type.id] }
end
_form.html.erb (Asset)
<%= f.select :asset_type_id, asset_type_all_select_options, :class => "input-text", :prompt => '--Select-----' %>
and here are my models:
asset.rb
belongs_to :asset_type
asset_type.rb
has_many :assets
Using the above code I get the following error:
undefined local variable or method `asset_type' for #<#<Class:0x007f87a9f7bdf8>:0x007f87a9f77d48>
Am I doing something wrong? Will this method not work with double barrel model names? Any pointers would be appreciated!
The variable asset_type in your assets_helper file is not defined. You would need to pass it in to the helper method
def asset_type_all_select_options(asset_type)
# ...
end
Or use an instance variable that you define in the controller (e.g. #asset_type).
However, you can simplify this by using the #collection_select form helper.
_form.html.erb (Asset)
<%= f.collection_select :asset_type_id, AssetType.all, :id, :name, { prompt: '--Select-----' }, class: 'input-text' %>
Take a look at the API for #collection_select for details.
I can't remember on the code, how to create new object in nested form (via Simple-form).. It was something like: "something :new_object ..."
Thanks
Normallyc use:
<%= f.fields_for :object do |builder| %>
But you can use simple_fields_for, like this:
form_for #user do |f|
f.simple_fields_for :posts do |posts_form|
# Here you have all simple_form methods available
posts_form.input :title
end
end
Reference: http://simple-form.plataformatec.com.br/#usage/extra-helpers
It is initially using form_for, but check this thread: nested attributes in simple_form returns mass assignment error
Thanks to dropbox, I found it..
You need add this to your javascript (in CoffeScript)
#= require jquery_nested_form
And that's the form (in HAML)
= simple_nested_form_for #variable do |f|
= f.input :code
// Link to create new empty object
= f.simple_fields_for :nested_attributes do |s|
= f.link_to_add "Add new", :nested_attributes
= s.input :name
= s.input :locale
// Link to remove
= s.link_to_remove 'Remove'
= f.button :submit
I have seen RailsCasts#302 which describes about the in-place editing using the best_in_place gem. Over there in for gender option Ryan uses the array inside the show.html.erb and makes it a dropdown box(see the gender section where he explicitly defines an array).
<p>
<b>Gender:</b>
<%= best_in_place #user, :gender, type: :select, collection: [["Male", "Male"], ["Female", "Female"], ["", "Unspecified"]] %>
</p>
But what I want is I have defined an array inside the Model itself like: (because my array elements are not simple and short in count)
For eg:
user.rb
class User < ActiveRecord::Base
def authencity_types
['Asian', 'Latin-Hispanic', 'Caucasian']
end
end
How am I going to use this array elements as dropdown using the best_in_place syntax.
PS: I did try something like this
<% #users.each do |user| %>
<%= best_in_place user, :authencity, type: :select, :collection => User::authencity_types %>
<% end %>
But it says undefined method authencity_types
You're defining an instance method on the User model, so try this.
<% #users.each do |user| %>
<%= best_in_place user, :authencity, type: :select, :collection => user.authencity_types %>
<% end %>
Alternatively you can define that as a class method like this.
class User < ActiveRecord::Base
def self.authencity_types
['Asian', 'Latin-Hispanic', 'Caucasian']
end
end
Or you may want to consider using a constant if it does not need to be dynamic.
I have the following in a partial in a Rails 3.2 app using formtastic gem
<%= f.semantic_fields_for :bucket do |bucket| %>
<%= bucket.inputs do %>
<%= bucket.input :bucket_name, :collection => #buckets,
:include_blank => false %>
<%= bucket.input :sub_directory, :collection =>
#buckets.first.paths,
:include_blank => false %>
<% end %>
<% end %>
right now in my controller I can get :bucket_name and the value is ==
to an integer, In my case I only have one item in my collection but it is giving me a value of 2. My guess is this is the ID value of the object.
it's important that I get the actual :name string value of the object
that is selected in the collection. I am not sure how to do this. so
let's say the item I select has a label of "my label" and it's the 3rd
item in the collection. how would I grab the value "my label".
By default, select inputs will use the id attribute of the model as the value attribute of the <option> tags, and it ties various methods on the object for the contents of the option tag such as to_label, name, and to_s.
You can change both with the :member_value and :member_label options respectively (these were called :value_method and :label_method in older versions.
The details of each option are in the documentation for the select input, which I assume you haven't looked at :)
http://rdoc.info/github/justinfrench/formtastic/Formtastic/Inputs/SelectInput
:member_value and :member_label are deprecated from v3 onwards of Formtastic.
Easiest way is to modify the collection passed into the input. See the example from formtastic github page
f.input :author, :as => :select, :collection => Author.pluck(:first_name, :id)
Here the first_name is the label and id is the value for the select options
I want to iterate through an array of objects
<% #users.each do |user| %>
<%= render "member_list" %>
<% end %>
My question is how do I pass the user object to the partial, and how do I reference it in the partial. I know how to do it if it's just a single object, but I don't know how to pass the single object from the array to it.
I tried passing user to it and reference user in the partial, but it doesn't recognize user in the partial.
You might do it without loop - pass it as a :collection parameter. To use a custom local variable name within the partial, specify the :as option in the call to the partial:
<%= render :partial => "member_list", :collection => #users, :as => :member %>
With this change, you can access an instance of the #users collection as the member local variable within the partial.
<% for user in #users %>
<%= render "member_list" , locals => {:user => user}%>
<% end %>
in partial:
Hello. I'm <%= user.name %>