How to turn Template has_many questions into a form - ruby-on-rails

User story: User can create a Template. Each template has_many questions. User has_many templates. Template and Question belongs_to user.
Right now the user can create questions for a template. User clicks on template and they see a list of questions.
Problem:
How do i turn this into a form where the user can answer the questions. The user needs to be able to use the template and infinite amount of times. A user must be able to click what template they want to use then fill out the form.
Thank you for any assistance.

Although your question is a bit vague, it sounds like you need to set up a couple tables to hold answers to your questions, which also belong to a user. I would recommend a table to hold a reference to all responses for a given template: i.e. belongs_to :user, :template. Then, create a table to hold answers to each question, maybe called answers? Each row would belong to a user and question and thus a given 'template' with a through: option set on a has_many association. Does that make sense?

Related

how to continue after the nested model forms railscasts?

I followed the railscasts nested model form part 1, making some changes to have it work in rails 4. Basically, I created 3 models: Quiz, Question, and Answer, and they all belong_to the model intuitively above them. A form in the new action is used to create the quiz itself.
However, I'm at a bit of loss on how to proceed now. After creating the quiz, the show view looks like this:
done by iterating through #quiz.questions and #quiz.questions.answers and just displaying them on the page with their respective content attributes.
That's great for displaying just the questions and answers, but it doesn't accept user input at all. How do I make it so the user can use radio buttons to select an answer, and have it submit SOMEWHERE to save the results for grading and future reference for the user?
I've actually tried to create a form simply within the show action and have it save to another model but I got stuck extremely quickly. I'm finding it really difficult to both display the results AND accept user input for the displayed results. I also can't figure out a good way to save this data. Making another 3 models with the equivalent of Questions having something like a user_answer attribute seems difficult to implement and messy. I'm a beginner of the grandest caliber so any help would be great!
Nachime, your data model is good so far from what I can tell. Now you will want to link the answers to the users via many-to-many relationship. Note: this assumes that users will only take each quiz once.
Your next data model version will have a users_answers table containing user_id and answer_id. You can access the relative models using the has_many :through association so that each user has_many :answers, through: :users_answers.
The quiz will essentially just link the logged in user to the array of answers selected.

Rails dynamic & modifiable form schemas

I have a problem with forms. I'm developing an application that allows applicants to submit applications, and I want to make sure that users (people who receive those applications) could add/delete questions in their applications. How would I implement that?
My thought is to have a Application model and a ApplicationQuestion model that belongs_to Application. But I'm pretty lost beyond that.
UPDATE:
Another issue is confusing me. In my systems I could get Admins to add/delete ApplicationQuestions dynamically, and I have no problem implementing that. However, I want the Users to see the current set of ApplicationQuestions and answer them without being able to add/delete/edit questions. I'm now using a proxy model ApplicationQuestionAnswer which belongs_to Application and ApplciationQuestion. However I'm still having trouble with the views.
I'm actually more concerned about the views. How should I render the
forms to form-fillers?
Start with an Application form containing a stable amount of Questions (refer to Rails nested forms to implement it). The basic implementation could look like that:
<%= form_for #application do |f| %>
<%= f.fields_for :application_questions do |aq_form| %>
<%= aq_form.text_field :question %>
<%= aq_form.text_field :question %>
Dynamic adding and removing questions could be achieved by adding (and removing) HTML blocks produced by Rails helpers shown above:
<%= aq_form.text_field :question %>
You could dig into it's implementation by yourself (and write some javascript to orchestrate HTML blocks addition/removement) or use some gem with that functionality. One of those popular solutions is cocoon. It provides you with pretty links (link_to_add_association and link_to_remove_association), which being clicked perform the desired dynamic actions. You may be intrested in trying a cocoon demo application to get an idea of how to embed it into your project.
Yes you can keep the models as you are thinking. There will be a User model which would have two roles basically. One would be the teacher (just taking an example) who asks the questions and one would be student who will answer those questions. Now the teacher would be able to create application having different questions so two more models Application and ApplicationQuestion. The relations can be:
Application belongs_to User
ApplicationQuestion belongs to Application
Now the student will answer the questions which can be stored in answer table which would be another model ApplicationQuestionAnswer which would also belong to User and ApplicationQuestion. Now the questions can be of different type (numeric, string, date etc) so take care of that, they all can be stored in a single column by handling it at the code end and for this also have a question_type attribute in the ApplicationQuestion model.
Now the teacher can edit the application and delete the questions if they want or add new. Be sure to add dependent destroy so the answer would also be deleted when the question is deleted. Also suppose if the question can have choices then be sure to add one more model ApplicationQuestionChoice which would belong to ApplicationQuestion and its id will be saved as the answer of the student.
Update:
The ApplicationQuestionAnswer model will save each question's answer instead what you can do is that have two models. One would be ApplicationAnswer and another ApplicationQuestionAnswer. And the associations would be:
ApplicationAnswer belongs_to User
ApplicationAnswer belongs_to Application
ApplicationQuestionAnswer belongs_to ApplicationAnswer
Now in the view the form would be for creating/updating ApplicationAnswer and having nested forms for its associated ApplicationQuestionAnswer which you will display to the students. And this will save all the answers.
Also the same nested forms can be used with the teacher to add or remove questions. You can also use any gem for nested form too:
cocoon
nested_form
Hope this helps.

How do I have one user define fields for another user to fill out?

I have one type of user creating form fields for a job posting. They can define the field type and name (eg. type:"string" and name:"job_title").
My question is how do I store this data so that the job form for the applicant user has all of these defined fields?
Currently the job and form template is organized like this:
job BELONGS TO employer_template
employer_template HAS MANY jobs
employer_template HAS MANY template_fields
I've tried to search for solutions, but haven't seen anything that answers it clearly enough.
I would avoid to create all those tables, it sounds an overengineered solution.
You could simply serialize the fields and the contents, and store them in just one field.
That is automatically done by serialize:
class employer < ActiveRecord::Base
serialize :custom_fields
end
Simple and fast :)

Rails: should I use STI?

I want to present my case, and know whether or not I should use STI solution.
I am creating a message-board website and so far I have couple of Models: User, Topic, Post..
to make it clear: Post is like a comment for a Topic. Topic has title and content. Post has only has content.
Now, the User has the option to Like/Dislike a Post or a Topic.
I thought about three options:
Topic and Post don't have a connection (each Model has "num_of_likes", "num_of_dislikes")
Topic inherit Post.
Topic and Post inherit from a Base Model which can be called LikeableObj for example.
Which of those three options is the most suitable for my needs?
Is there a fourth option I didn't think about?
What if I'd like in the future to have a third Model which can be Liked?
I assume you'll want to keep track of whether a user has liked a certain post or topic, so I would make a join model for likes that connects a user to either a post or topic.
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :liked_obj, polymorphic:true, counter_cache:true
end
Since the liked_obj is polymorphic, it can be a post or a topic. You can then put has_many :likes on those models and a column likes_count, which will be updated automatically as a counter cache.
If you have any code that deals with likes that is common between Post and Topic, put it in a module Likeable and include it in both classes.

Rails named has_many with a limit

I want to achieve a has_many association with a x number of records, and the records would be named.
Let's explain that better. In a previous question I asked how to make a text area with a selectable markup language and we reached the conclusion that I needed a separate model, Field which had the multiple fields I needed (language, original and rendered).
Now I want to be able to make a model, let's say User, which had two of these fields. For example: about_me and biography. How would I create those fields every time I create the user, edit them when I edit the user and destroy them when I edit the user? And how would I display them simply writing: User.about_me and User.biography?
Thanks in advance for the answer.
David's solution creates the joint model.
Then you have to include the form of the profile in the User form. You'll have to use accepts_nested_attributes_for method in the User model.
To destroy the profile when user is deleted, add dependent => :destroy to the relationship between the 2 models.
You'll need to use callbacks (http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html).
For example, in your User model, have a after_create callback that will create the requisite fields.
Also,have an after_save callback that checks user.changed? and if it is different, update the fields.

Resources