I am creating an application where I want to add metadata about table fields from an enterprise system.
I have a table_structure model which retrieves a table definition information like:
table_name
field_name
Field_type
field_length
...
a particular field may exist in multiple tables like:
tableA
fieldX
tableB
fieldX
regardless of table, I want to add attributes to the field so that
fieldX :has_many :attributes
and the attribute model would be
:field
:attribute
:value
I would like to create a single form where I can capture many attributes. I've seen the nested forms railscast and that's close to what I want to do, but I would like to have the form generated dynamically with different input types because the attributes captured may change.
I was thinking of adding this method to the attribute model and somehow iterating through them and generating the form.
def self.attributes_types
{'Business Essential' => {:field_type=>:radio,:values=>[:y,:n,nil],:default_value=>nil}}
{'Owner' => {:field_type=>:text}}
end
Is Nested form the way to go? I am not adding fields, just attributes to fields, so I can pass a params[:field] to new and use that for my new attribute(s). Is there another way to create this form?
I think you're on the right track and nested fields for attributes are the way to go. If new attributes are going to be introduced in the future, you might want to store attribute definitions in a database table instead of defining them in the model.
I don't normally recommend document-oriented database systems but this may be a good candidate for MongoDB instead of a traditional SQL backend. Regardless of the backend, nested forms are the way to go. You can build some helpers to dynamically add them to your forms based on the metadata stored in your database.
What your are looking for are called dynamic forms and the answer to your question is answer in 403-dynamic-forms.
http://railscasts.com/episodes/403-dynamic-forms
https://github.com/railscasts/403-dynamic-forms
Its kinda late but i hope it helps someone else
Related
I am currently working on a simple rails4 app. As part of the app, I am creating a form to populate the database and a particular column (:additional), I would like to populate with a hash where the key is a string (heading) and the value an array of strings (paragraphs below heading). So, for example: {"Heading" => ["Paragraph1", "Paragraph2"]} etc.
I am confused how I would now set up a form using rails to populate this column. I was thinking of creating a text_field for the title and then one or more text_areas underneath for the paragraphs and then somehow merging them in the controller but when creating the fields, I have to give the object as :additional which leads to problems.
How would I go about best accomplishing this? Is it even possible or should I restructure my database somehow?
Any advice is much appreciated.
If you're using postgres, ActiveRecord has support for using :hstore as the column type. If you're not, you can use serialize.
I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly
Is there any gem/plugin for ruby on rails which gives the ability to define custom fields in a model at runtime with no need to change the model itself for every different field.
I'm looking for something like Redmine acts_as_customizable plugin which is packaged as a gem usable in the rails way, i.e.
gem 'gemname'
rails g something
rails db:migrate
class Model < ActiveRecord::Base
acts_as_something
end
Here are the CustomField and the CustomValue classes used in Redmine.
Edit:
Since my question is not clear I add a brief use case which explains my need better:
I want users to be able to design their own forms, and collect data
submitted on those forms. An important decision is the design of how
these custom dynamic records are stored and accessed.
Taken from here, in this article approach the problem with different ideas, but they all have drawbacks. For this reason I'm asking if the issue has been approached in some gem with no need to rethink the whole problem.
I'm not aware of a gem that does this, but serialize works quite well and it's a built-in. You get a NoSQL-ish document store backed by JSON/YAML.
If you allow user to create a custom form, you can pass nested arrays et cetera directly into the attribute. However, if you need to validate the structure, you're on your own.
I'm afraid it could be tricky and complicated to do it in ActiveRecoand (generally in standard relational database). Take a look at http://mongoid.org/docs/documents/dynamic.html - this mechanism is using nosql feature.
You can also may try the following trick:
1/ Serialize a hash with your custom fields in the database column, for example { :foo => 'bar', :fiz => 'biz' }
2/ After load a record from database do some metaprogramming and define corresponding methods on the record's singleton class, for instance (assume that custom fields are stored and serialized in custom_fields column):
after_initialize :define_custom_methods
# ..or other the most convinient callback
def define_custom_methods
# this trick will open record's singleton class
singleton_class = (class << self; self; end)
# iterate through custom values and define dynamic methods
custom_fields.each_with_key do |key, value|
singleton_class.send(:define_method, key) do
value
end
end
end
Since rails 3.2 you can use store method. Just include following in your model:
store :properties, accessors: [:property1, :property2, :property3...]
You only need to change your model once (to add properties field to db table). You can add more properties later without altering the schema.
The way this works is by serializing properties hash into YAML and saving it into database. It it suitable for most cases, but not if you'd like to use these values in db queries later.
I don't know a gem, but this can be accomplished be creating a table called custom_fields with a name column and possibly a datatype column if you wanted to restrict fields by datatype.
Then you create a join table for a custom field to your desired table and a value and do whatever validations you want.
Rails3,jQuery. What I'm trying to do: Create a new WorkoutPlan that HABTM Exercises. Then create a new Workout that belongs_to a WorkoutPlan. I want the Workout#edit page to build a form with fields_for new/edit WorkoutRoutines, one routine for each exercise in the WorkoutPlan.
That last part is where I'm struggling, creating one Routine for each Exercise in the WorkoutPlan. Everything else works, its just this form build that sucks. Not sure if I'm making this overly complicated, or if there was an easier way. Any ideas?
From what I understand, using nested attributes should solve the problem without many complications. From the link,
Nested attributes allow you to save
attributes on associated records
through the parent. By default nested
attribute updating is turned off, you
can enable it using the
accepts_nested_attributes_for class
method. When you enable nested
attributes an attribute writer is
defined on the model.
The attribute writer is named after
the association, which means that in
the following example, two new methods
are added to your model:
author_attributes=(attributes) and pages_attributes=(attributes).
anytime you need a complex form in rails its either going to be a pain, or can use formtastic. super easy awesome forms, plenty of ways to deal with habtm relations as well.
I have a form that I need to display for a table that has a relationship with a couple of other tables. For instance, I have a table "cases" (think, investigator case), that has_one claimant and has_one client.
I would like the form to display the fields to fill out for the case data, client data and claimant data. Is there an easy way to do this so that when it's submitted, it would be as simple as:
case = Case.new(params[:case])
case.save
As it would be if I was just submitting and saving a form for only the case data?
Sounds like you are looking for the accepts_nested_attributes_for method of activerecord. You will need to craft your form using
- form.fields_for :claimant do |claimant_form|
= claimant_form.text_field :name
You can find much more information in Ryan Daigle's blog post
I don't believe there's a way where you can just call case.save and it'll work.
To make the form, look into using fields_for. http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001605
fields_for allows you to add fields that are stored in different POST variables, so if you set up the fields correctly then in your new method you could potentially do something like this:
claimant = Claimant.new(params[:claimant])
claimant.save
Which isn't terribly more complicated.
See my complex-form-examples on creating nested multi-model forms. This has been updated to work with Rails 2.3's accepts_nested_attributes_for.
It allows you to nest the associations all under params[:case] like you want. When you call case.save everything else will be saved too.