Ruby on Rails Monday, March 28, 2011

Hay all!
I have question-answer base.. like quiz, yeah? yes!
So i need to do this way... my project without registration so this way
i need token..
user can answer on my question only once.. How can i do this? Iand i can
give him link, and link should be like : localhost:3000/token .. he gets
on this link, answer and click go and that's all. If he want to get on
this page once again he will get message something like "Sorry bro you
already passed quiz".

#answer_controller.rb
class AnsweController < ApplicationController

layout "default"

$all_questions_archive = Question.count('id')
$all_questions_active =
Question.find(:all,:conditions=>"enabled=1").count

def create

@w1 = rand(9999999)
@w = Digest::SHA1.hexdigest(@w1.to_s)


@answers=Hash.new
questions = Question.find(:all,:conditions=>"enabled=1")
questions.each do |question|
if params["answer_"+question.id.to_s]
@answers[question.id]=params["answer_"+question.id.to_s]
if
!Answer.create(:question_id=>question.id,:token=>@w,:text=>params["answer_"+question.id.to_s])
flash[:error]="Can`t create answer!"
end
end
end
if !flash[:error]
redirect_to :action=>"pass"
return false
end

end

def index

@answers=Hash.new
@questions = Question.find(:all,:conditions=>"enabled=1")

end


def pass
flash[:notice]="<span id='ty'>Thank you for your answers!</span>"
render :text=>"", :layout=>"ty"
end


#view => index.html.erb

<% form_tag :action=>"create" do %>
<table border="0" width="900px" align="center"
style="background:#e1e3e3;
padding-top:10px;padding-left:20px;padding-right:20px;padding-bottom:20px;">
<% @questions.each_with_index do |question, index| %>
<tr style="vertical-align:middle; text-align:left;">
<td width="400px" colspan="2" style="padding-top:10px;">

<span style="font-size:15px"><%= index+1 %>. <%= question.text
%></span><br>
<%= text_area_tag "answer_#{question.id}",@answers[question.id],
:cols=>"90", :rows=>"4", :class => 'inputBox' %>
</td>
</tr>
<% end %>
</table>
<table border="0" width="900px" align="center" class="table2">
<tr>
<td align="right"><%= submit_tag 'Pass survey &rarr;', :class =>
'submit'%></td>
</tr>
<tr>
<%=h @w %>
</tr>
</table>
<% end %>


What did i do wrong? Please help m,e!

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment