Actualización de contenido sobre la marcha en Rails

I have an app that allows users to create profiles and I'd like them to be able to update their profiles in the profile view rather than going to the edit page. Here's an example from OkCupid:

This can be done using the bootstrap-x-editable-rails gem, but I'd like to know if I have to create a database column for each question? Or if I can somehow just use one - I guess the question is how can I achieve something like the example above in rails?

¡Gracias de antemano!

preguntado el 02 de diciembre de 13 a las 15:12

Use a column for each question. Update the columns as needed. -

1 Respuestas

Each column(field) for each question is a bad idea.

What if you want to add question? Add another field? What if you want to change question? Also how to define the name of the fields? Wouldn't you need a hash in model to translate them into real question? What if you decide to allow user to add their own question?

A better approach is to define a Question model.

The basic idea is, this model belongs to User, so it's possible to let every user add their own questions later. Initially, you can set basic questions belongs to a certain admin or better with an attribute public: true, and every user can see this kind of questions

Then, user also has Answer model. Each answer belongs to a certain question. That's intuitive.

Then, done.

Respondido el 02 de diciembre de 13 a las 16:12

In the case where you may change questions, you may be better served for the Answer model to contain a copy of the current state of the Question in addition a belongs_to relationship. That way, if a question is restated the user can see what they originally answered in addition to the current state of the question. - andyv

Thanks AndyV, that's a really good idea. How might I accomplish this? - hijo negro

@AndyV, nice point. About the changing issue, another approach I can think of is, once the user answered a question, the system add this question from public to his own, so it's always fixed. - Billy Chan

Thanks @BillyChan, answer accepted. Just to clear one part up - what do you mean by " once the user answered a question, the system add this question from public to his own, so it's always fixed " sorry not sure if the system is a typo or I just mis-understood it. - hijo negro

@SonnyBlack, thanks. Let's say there is a question "What's your favorite food" with id 2. This question belongs to admin and is public. So, an user Joe can see this question in his profile, with default blank answer information. Now, Joe decides to answer this question. Once he submit his answer, the app will copy this question at first, from id 2 to id say 20, from belonging to admin to belonging to Joe, from public to private. Then his answer will associate with this new question id 20 belonging to Joe. In the future, if you change the question to "What's your favorite Italian food", - Billy Chan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.