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.
Related
I have an rails application where I have scaffolded out an entire resource. Now through the UI I am trying to create an record but somehow i have an column in my database which is of type JSONB (Supported by PostGreSQL) whose value is not getting pushed into my table. Can anybody help me out on this as i am relatively new to rails.
I do not understand your intention of using the jsonb through the form.
you normally save a hash in jsonb column. So if you put hash like following in your text field it should get saved.
[{name: 'Jone', lastname: 'Doe'}].
Hope that helps
I'm using Mongoid 4.0.0 with Rails 4. My models map tables in another application, and I have no control over the field names.
One of the models has a field named id, which is getting coerced into Mongo's _id field. For example, when I insert a document with an id value of "something" I get
{_id:"something", id:null}
instead of
{_id:ObjectId("<hexstring>"),id:"something"}
Is there any way to avoid this coercion, make Mongoid not conflate the two fields, and leave my id field alone?
As I said, renaming the id field is not an option.
Thanks!
[edited]
This is definitely not a MongoDB issue. It must be in Moped or (my guess) Mongoid.
I've tried changing the params key from :id to :_rid but this is still happening. I'm going to check out aliases, but from my first pass I don't think they're going to help -- they appear to go the wrong way.
This appears to be hardcoded into Moingoid and a pervasive assumption throughout. It's annoying enough, though, that I might come up with a patch to allow users to override the key field on a per-model basis.
Oh well.
Here's my challenge. I have a key/value set that I want to tie to a model. These are my specific requirements:
I want the hash to be stored as a serialized JSON object in the model's table instead of in a separate table
I want to be able to pre-define the valid keys within the model itself
I want to be able to set a strong type for each key and automatically perform validations. I don't want to have to write validation functions for each individual attribute unless it needs a validation out of the basic data type scope.
I would LOVE to be able to magically access the attributes inside a form generator (f.input :my_key) and have the form generator recognize that :my_key is of type :boolean and create a checkbox instead of a generic text input. The same for other data types.
There are a few different ways to solve this problem, and lots of opinions for both. I read over this answer from 5 years ago:
Best approach to save user preferences?
It seems that many/most of those plugins have been abandoned. Anything else come out in the last 5 years that matches my criteria?
Your question is a bit open-ended, but as far as I can see your needs, they should be met with using Hashie gem.
I am working on an application that has a project model that contains tags. The model roughly looks like:
class Project
include Mongoid::Document
field :name, type: String
field :tags, type: Array
...
...
end
I am trying to figure out the best way to make a form for creating/editing a project that would allow a user to add multiple tags. I would like to use rails built-in form_for method, if possible.
The issue I am coming across is that I am unsure how to give the user the ability to add multiple tags.
One idea would be to use a string field, and then before the project is created/updated, I could split the string on comma's, or spaces. I could also easily create a text area and split on a new line character.
However, I would much rather give the user the ability to add tags by typing a value into a field, and then clicking a plus symbol to append it to an array of tags. I can easily accomplish this with javascript, but I was wondering if a solution exists, whether it be through another gem or using an existing rails method, that would not require me to write custom javascript for the form?
Any help or pointers would be appreciated.
Thank you
To generate an array with params you can do by define a "[]". So you can do :
text_field_tag "project[tags][]"
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