Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to create attributes dynamically in rails?

ruby-on-rails dynamic_attributes

  • Please log in to reply
1 reply to this topic

#1 Logesh

Logesh

    Signalman

  • Members
  • 14 posts

Posted 18 November 2013 - 07:33 AM

I have a requirement where i need to create form with dynamic attributes but i am stuck with the way to proceed. Actually my requirement is similar to a CMS. I am using devise for authentication and once the user is authenticated. I need to list a names of the collection that the user has so far created. And clicking on the new button should take the user to the form where he should be able to give the field_names ( first_name, last_name, age ) and  type of the field ( string, string, integer ) and on adding all those required he can save and which would take to the another page where the fields added should be displayed as the column name and the user should be able to add values to the column. How can i do this?

 

I hope you can understand my requirement. If you have any question or if you cannot understand i can explain you more. 



#2 Kelli Shaver

Kelli Shaver

    Inspector

  • Administrators
  • 75 posts
  • LocationKentucky

Posted 27 November 2013 - 03:17 AM

Dynamic forms are tricky and IMO they fall into the realm where a NoSQL solution is better, due to their fluid nature.

 

I haven't actually had a need to do this yet in Rails (though I have in Sinatra), but off the top of my head, you'd want to store the schema for the form in the database for the purposes of generating said form. Then the records themselves would be stored in a key/value store - I'd say use Postgres so you get things like JSON support in the DB and hStore column types). You could then delegate the attributes (keys) to the model when it's instantiated, so you could access the data as User.first_name, User.last_name, etc. 







Also tagged with one or more of these keywords: ruby-on-rails, dynamic_attributes

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users