Rails 4 simple Quiz web app using Cocoon - ruby-on-rails

My current task is to create simple Quiz web app. App will be structured this way:
- Admin can create, edit, delete and activate quizzes.
- Visitors can take a quiz and submit it. Then receiving quiz results etc.
So far I have created nested form (with Cocoon gem) where admin can create quiz's and add new questions and possible answers, but I have not a single idea how to make that nested form as submit-able quiz for visitors.
My code:
Form:
<%= form_for [:admin, #test] do |f| %>
<div class="field">
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<h3>Questions</h3>
<div id="question">
<%= f.fields_for :questions do |question| %>
<%= render 'admin/question_fields', f: question %>
<% end %>
<div class="links">
<%= link_to_add_association 'add question', f, :questions %>
</div>
</div>
<%= f.submit %>
<% end %>
_question_fields partial:
<div class="nested-fields">
<div class="field">
<%= f.label :description %>
<br/>
<%= f.text_field :description %>
</div>
<%= link_to_remove_association "remove question", f %>
<h3>Answers</h3>
<div id="answers">
<%= f.fields_for :answers do |answer| %>
<%= render 'admin/answer_fields', f: answer %>
<% end %>
<div class="links">
<%= link_to_add_association 'add answer', f, :answers %>
</div>
</div>
</div>
Question: What is the best way to allow dynamic nested form to be used for taking tests by visitors and store submitted data in database?
Thanks in advance.!

Related

Nested route values not populating edit Rails

I have a belongs_to relationship between a map and a row. A map has_many rows.
For some reason, when I try and edit a row, even when it is populated in my database, it is not showing the values when I try to edit this row. Why would this be? Below is the _form.html.erb and the edit.html.erb file.
Edit.html.erb
<%= render 'form', row: #row %>
_form.html.erb
<%= form_for [#map, #map.rows.build], method: :post, url: map_rows_path do |form| %>
<div class="field">
<%= form.label :timeframe %>
<%= form.text_field :timeframe, id: :timeframe %>
</div>
<div class="field">
<%= form.label :standards %>
<%= form.text_field :standards %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_field :content %>
</div>
<div class="field">
<%= form.label :skills %>
<%= form.text_field :skills %>
</div>
<div class="field">
<%= form.label :resources %>
<%= form.text_field :resources %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
If I try Row.last in the rails console it confirms that this row is populated with data. I am assuming this has something to do with the way the form is setup however I am not familiar with these relationships. How can this be fixed?
it seems you are passing row to form where <%= render 'form', row: #row %> but in Actual edit form you are using a variable, #map and building a blank row object by saying #map.rows.build, hence the row object will be a new object with out any values.
Based on your code change it to following and see. ( I am assuming you are setting up #map object in your edit action in controller)
<%= form_for [#map, row], method: :post, url: map_rows_path do |form| %>

Date is not working in rails application with mongo id

I have create an rails application with mongoid but im facing one problem at DATE
I created an scaffold with a name "posting"
When im editing date it will updated....
I follow the instruction of Railscast #238 Mongoid here
there is my posting.rb file
class Posting
include Mongoid::Document
field :title
field :description
field :comments
field :published, :type => Date
end
this my _from.html.erb
<%= form_for(#posting) do |f| %>
<% if #posting.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posting.errors.count, "error") %> prohibited this posting from being saved:</h2>
<ul>
<% #posting.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.date_select :published %>
</div>
<div class="field">
<%= f.label :comments %><br>
<%= f.text_area :comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and finally my show.html.erb file
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #posting.title %>
</p>
<p>
<strong>Description:</strong>
<%= #posting.description %>
</p>
<p>
<strong>published:</strong>
<%= #posting.published %>
</p>
<p>
<strong>Comments:</strong>
<%= #posting.comments %>
</p>
<%= link_to 'Edit', edit_posting_path(#posting) %> |
<%= link_to 'Back', postings_path %>
What do you mean by not working? doesn't look like you have used published property in any of your views.
in your show.html.erb you are using
<%= f.date_select :pub %>
and in your show.html.erb you are using
<%= #posting.pub %>
However there is no property called pub in your Posting model. What you have there is called published
field :published, :type => Date
You either need to rename it in the model, or in the views to match.

undefined method for form fields

I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.
When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!
/searches/new.html (for search):
<%= form_for #search do |f| %>
<div class="field">
<%= f.label :keywords %><br/>
<%= f.text_field :keywords%>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
/users/new.html (for the user profile):
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br/>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :username %><br/>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :zip_code %><br/>
<%= f.text_field :zip_code %>
</div>
</div>
<div class="field">I'm a
<%= f.select :gender, ['man', 'woman']%>
interested in
<%= f.select :gender, ['women', 'men', 'both']%>
</div>
<div class="field">
Age <%= select(#object, :age, (18..75).to_a) %> to <%= select(#object, :age, (18..75).to_a) %>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
Your #search object is a Search object (or whatever it actually is), not a User.
Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.
There are any number of ways around this, including giving your Search a User property.
You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.

Ruby Rails form

Hi there I'm extremely new to Ruby and I'm currently working with two models and two corresponding databases. A Quiz which has_many questions. And I'm trying to get the questions to show up in the quiz "edit view" here's what I have.
What currently is not working is the form entry for "questions" (see commented part below). I don't think I understand the expressions preceded by the colons very well. (e.g. :title, :quiz_date etc>) are they regular variables?
Anyway, the block of code in question successfully makes a form but using :questions it puts all the information from the questions row of the database into to the form filed (i.e id, question, answer, possible answers, etc). But it doesn't give me just the value of the question field.
But if I change it to :question (hoping to just get the value of the question field in the questions table) I get an error. I also tried :questions.question and just plain questions.question. None of these worked.
Any suggestions?
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved: </h2>
<ul>
<% #quiz.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :quiz_date %><br />
<%= f.date_select :quiz_date %>
</div>
<div class="field">
<%= f.label :reading %><br />
<%= f.text_field :reading %>
</div>
<div>
#the problem code
<% #quiz.questions.each do |questions| %>
<div class="question">
<%= f.label :questions %><br />
<%= f.text_field %>
</div>
<% end %>
#end problem code
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You are going to need to adds accepts_nested_attributes in your Quiz model
Class Quiz < ActiveRecord::Base
accepts_nested_attributes_for :questions
Then in your form add
<%= f.fields_for :questions do |build| %>
<div class="question">
<%= build.label :questions %><br />
<%= build.text_field :questions %> #whatever attribute in your questions model
<%end%>
You will find this Railscast very helpful, http://railscasts.com/episodes/196-nested-model-form-part-1

Rails - Use the same form with new/edit using fields_for

I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.

Resources