Sunday, March 21, 2010

doing a survey with ruby on rails (part 2)

Now, that the tables and models are setup, we implement the View and Controller layers. The scaffold has given already some start, but we'll change these to give our application a better survey character.

First, the index view of the questions. We want to see a simple list of questions that we have so far, and the associated choices.

So, let's edit this view in /survey/app/views/question/index.html.erb. We remove almost everything and replace it with a unlinked list in html like this:



Note how we have used a second unlinked list to display the choices that are associated with a question, and how the modify and destroy actions are removed to be accessible only from inside the question edit. The question can be edited by clicking on it.

At the moment, we don't have any choices yet in our database. Let's add some with putting "http://0.0.0.0:3000/choices" in the URL of our browser.

I add the following for now: "it's great", "much", "ok", "not much", "green", "blue", "red", "yellow"

Now, we arrive at one of the more difficult parts. Putting checkboxes in the new and edit views of the questions.

First, the new action in /survey/app/views/questions/new.html.erb :

We need to iterate over the choices. We can do this like this:




We also insert the loop in the edit view:




To have a short list in our show view on choices, we add in app/views/questions/show.html.erb:



So, that was part 2. By now, you should have a survey app where an administrator can easily enter questions and associate possible choices with these questions.

Next, we need to have users who can take part in the survey.


3 comments:

Katie said...

Thanks for your helpful survey posts! Have you built the user-interface for taking the surveys yet? I am stumped!

Anonymous said...

thanks for the comment.
my work on the survey view did not very far, because I was not quite sure about what I want to have the survey. I recently found some nice examples, I'll let you know soon

Katie said...

Great! Can't wait...I've been hitting a brick wall with this for weeks!