Create a client side (React model) to “reflect” the rails response. For example, on the side of the rails:
class Message < ActiveRecord::Base
belongs_to :thread
validate :text, presence: true
end
The JSON response sent by the index / new server endpoint should determine the structure of the data model on the client side. For example, the index endpoint may return:
{
"messages": [
{
"id": "1",
"text": "Hello Stack",
"threadId": "5"
}
]
}
Client Side Model:
var Message = function Model(){
this.id = null;
this.text = null;
this.threadId = null;
};
Message.fromJSON = function(messageJSON) {
var message = new Message();
message.id = messageJSON.id;
return message;
};
module.exports = Message;
Then when creating / editing just call JSON.stringfy (msg).
source
share