how to continue after the nested model forms railscasts? - ruby-on-rails

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.

Related

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.

Submitting two tables/model data with single/two form on one webpage with single submit button?

My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Handling multiple forms interacting with the same model in Rails

I have an app that is dealing with "patients". Each patient is going to have three forms that they deal with initially. Each form has related questions, but they also have differences, so they can't be the same form. Each answer, even if it is for the same question, has to be recorded and archived. Also, there needs to be some sense of versioning to each form that is filled out. For instance if patient "Steve" fills out "Form 1" on "October 5th", and a question is taken away from the form the next day, I need to still be able to pull up the questions that Steve filled out on Form 1. Right now I have four models that I think solve this problem which are:
patients: first_name, last_name
forms: name, version
form_response: patient_id, form_id
questions: content, form_id
answers: response, question_id, form_response_id
Would this be the best way to map out this data set? Also, should this all be handled through the patient model? If not, what type of model structure should be used to handle this? I'm just really confused on how to best handle this situation as far as what should go where and not end up with a mess of code to maintain. Thank you in advance for your help.
You have the right ides. Starting from the top:
form
has_many questions
questions
belong_to form
has_many answers
answers
belong_to question
belong_to response
Then you tie it together with the response:
response
belongs_to patient
belongs_to form
I would suggest looking into form objects to make this a lot easier. You might want to checkout:
Form-backing objects for fun and profit
Rails Form Objects
Form Objects

Multiple non-related models in one Rails form

I am building a blog-style application in Rails 3 where multiple users are able to post some news. After the login (which is realized with "Authlogic") the user values are stored in a own model called e.g. "UserSession". The form for the post contains title, content etc. and the username should be stored with a hidden form.
I think that the two models don't need to be related to each other (by that I mean a :has_many - :belongs_to relationship) because there isn't any further usage for that information.
Do I really not need this relation? And how could I realize the form?
For Authlogic is it important to remember that the 'UserSession' does not correspond to any database tables (i.e. you would never use a has_many or has_one 'UserSession'). I think the relationship you are looking for is:
User has many Posts
Blog belongs to User
The reason? It is always a good idea to associate a record with the 'owner' so that the owner can later modify or delete the record. I hope this helps.

Friendship invite form with hobo

The hobo lifecycle tutorial shows how to implement a friendship logic in
the model and controller. However it does not really cover how to glue
the gui/views together. When I go to /friendships/invite - hobo
presents me with a form with a drop down menu. How do I add a form to
the user show-page with just one button (Invite) I guess that the the
user viewed should be in a hidden field?
I tried adding the form like this:
<extend tag="show-page" for="User">
<old-show-page merge>
<append-content-body:>
<invite-form for="Friendship" />
</append-content-body:>
</old-show-page>
</extend>
Hobo ignores the invite-form hmmm I must be missing something.
Best regards
Asbjørn Morell
try <invite-form:friendships.build/> assuming you have has_many :friendships on your user model. for is an attribute on the 'def' and 'extend' tags, it's not recognized by 'form'. To use a polymorphic tag, simply pass in an instance of the correct type in the context. This of course is easier if an instance already exists, but since you're using a creator action, just build one.
P.S. Questions like this get answered much quicker on the Hobo Users mailing list.

Resources