Direkt zum Hauptbereich

Evaluate checkboxes with params through post action in rails

Lately I was looking to evaluate the parameters from an post action.

The background is my questions model with four answers and one column with the a pointer to the right answer. My index.html view looks like this:


 <%= form_tag do %>  
 <table>  
  <% @questions.each do |question| %>  
      <tr>  
           <td>  
           <%= question.content -%>  
           </td>  
              <td>  
          <%= question.answer1-%>  
          <%= check_box_tag 'question_ids[]', question.id.to_s + '_1' -%>  
             </td>  
           <td>  
          <%= question.answer2-%>  
          <%= check_box_tag 'question_ids[]', question.id.to_s + '_2' -%>  
             </td>  
           <td>  
          <%= question.answer3-%>  
          <%= check_box_tag 'question_ids[]', question.id.to_s + '_3' -%>  
             </td>  
           <td>  
          <%= question.answer4-%>  
          <%= check_box_tag 'question_ids[]', question.id.to_s + '_4' -%>  
             </td>  
      </tr>  
  <% end %>  
 </table>  
 <%= flash[:message] %><br>  
  <%= submit_tag("Post") %>  
 <% end %>  

After I have checked some boxes a post action is done and per se the create method is called in the corresponding controller:

  # POST /diagnoses  
  # POST /diagnoses.json  
  def create  
      flash[:message] = params[:question_ids]  
       respond_to do |format|  
         format.html { redirect_to diagnoses_url }  
         format.json { head :no_content }  
        end  
  end  

The controller just puts the parameter on the screen as a flash message when the view is reloaded. NOTE: I put an extra flash message code in my view above.

Now I just have to evaluate it:
  1. split id and answer number
  2. compare answer number with column in model of given id


Kommentare

Beliebte Posts aus diesem Blog

rails + carrierwavve + plupload = multiple file upload

Right now I am very happy! I finished my work on creating a file upload method in rails for more than one file, altogether handled by rails sessions and as objects. First I had to learn a bit more about how rails works and I achieved that by reading a lot: books, screencasts and blogs. In this blog post I like to share what I accomplished, but without the hussle of putting code blocks in here, so watch out for bold formatted text as that is the code. My goal was to have one site with just the pure option of uploading files. All file handling would be done behind the scenes by myself logging onto my server. That said you are not going to see any fancy CRUD here. Lets start then! And dont forget to versioning your app BEFORE you try something new! [e.g. git] > rails g scaffold Photo title:string Now add carrierwave to you Gemfile and run > bundle install >rails g uploader Photo Alltogether you have model, controller, views and an uploader folder with the Ph...

LEGO Alternativen: Sluban und COBI

Ich habe vor kurzem wieder Feuer gefangen für LEGO. Und das kam durch die Dokumentation über die Errichtung des LEGO-Hauses in Billund. Also habe ich meine Steine gereinigt und die Kinder fangen gerade an, auch mal etwas zu bauen. Teilweise stammen meine Steine aus den 1970ern, 1980er und viel aus den 1990ern - alles passt noch wunderbar aufeinander. Viele Designs von LEGO gehen doch Wege, die ich selber nicht so toll finde. Ausserdem verstehe ich nicht, dass etliche Designs aufgegeben wurden. Letztere Sets findet man dann zu horrenden Preisen auf den einschlägigen Plattformen. Also sah ich mich um, denn das Patent für das Baustein-Prinzip ist abgelaufen und andere Firmen produzieren auch Stecksteine. Sluban ... ist Mist und wird von mir nie mehr gekauft. Die Firma stiehlt von LEGO, hat aber auch interessante Designs. Gerade bei militärischen Sachen findet der Fan einige gut aussehende Sets. Aber die Steine wirken nicht besonders toll, es gibt Macken, die Anleitung kön...

debian 6 vserver - a new installation due to centOS update problem - my steps

My centOS vserver broke down after a "yum update" and even the centOS community forum could not give an answer within 24h. In my books that is poor for the almighty and stable centOS. Bare in mind that I am just a normal user of linux, no professional and definitely no expert when it comes to server, but I can read, try and share my findings: debian 6 installation steps via ssh secure your server: adduser foobar for security purposes  change /etc/sshd/sshd_conf to PermitRootLogin no now you can login as foobar and change to root via su - visudo for sudo command permissions and put this at the bottom: foobar   ALL=(ALL) ALL change hostname change hostname in /etc/hostname and in /etc/hosts reboot login via rsa key rather than password locally do ssh-keygen ssh-copy-id -i foobar@hostname... ssh now works without passwords but with keys (for easy deployment) install ruby and rails via rvm login as root always good to have: sudo apt-get install...