Connecting Tech Pros Worldwide Forums | Help | Site Map

Ruby operator affect overall program readability

Newbie
 
Join Date: Nov 2008
Posts: 1
#1: Nov 21 '08
I am designing a form (in windows) and I wish to import the names of checkboxes from a database. Can please someone help me out with this?

Expert
 
Join Date: May 2007
Posts: 213
#2: Nov 21 '08

re: Ruby operator affect overall program readability


Hi, and welcome to bytes.

Do you have any code you are working with? Are you able to query the database, or are you having trouble with any specific piece of the program?
Newbie
 
Join Date: Nov 2008
Posts: 3
#3: Nov 28 '08

re: Ruby operator affect overall program readability


mysql_fetch_array is used to fetch the data stored in a database .i think you need the labels for the checkboxes to be fetched isnt it ? then you may do it using the above method .
Expert
 
Join Date: May 2007
Posts: 213
#4: Nov 29 '08

re: Ruby operator affect overall program readability


Quote:

Originally Posted by itcoll View Post

mysql_fetch_array is used to fetch the data stored in a database .i think you need the labels for the checkboxes to be fetched isnt it ? then you may do it using the above method .

Can you show an example using that with Ruby/Rails? Generally, with Rails, querying a database is much easier. See this page for a comparison.
Newbie
 
Join Date: Nov 2008
Posts: 2
#5: Dec 10 '08

re: Ruby operator affect overall program readability


Quote:

Originally Posted by improvcornartist View Post

Hi, and welcome to bytes.

Do you have any code you are working with? Are you able to query the database, or are you having trouble with any specific piece of the program?

Hi,

Thanks. :)
Actually i am a newbie in ruby and rails. I am having issues with code which can be used to call the data from the DB and put them as checkbox names in the form.
Can you please help me with this?
Regards,
Navneet
Expert
 
Join Date: May 2007
Posts: 213
#6: Dec 10 '08

re: Ruby operator affect overall program readability


Can you post your current code? Are you able to get the data from the DB? Do you have a model defined for the database table?
Newbie
 
Join Date: Nov 2008
Posts: 2
#7: Dec 11 '08

re: Ruby operator affect overall program readability


I have created a user model, but the subscribe checkbox is with the default site controller.

Expand|Select|Wrap|Line Numbers
  1. view
  2. subscribe.rhtml
  3.  
  4. <html>
  5. <head>
  6. <title>Subscribe</title>
  7. </head>
  8. <body>
  9. <h2>Subscribe</h2>
  10.  
  11. <% form_for :user do |form| %>
  12.  
  13. <fieldset>
  14. <legend>Enter Your Details</legend>
  15.  
  16.  
  17. <label for="%parameter1">Parameter1:</label>
  18. <%= form.check_box :parameter1 %>
  19. </br>
  20. <label for="%parameter2">Parameter2:</label>
  21. <%= form.check_box :parameter2 %>
  22. </br>
  23. <label for="%parameter3">Parameter3:</label>
  24. <%= form.check_box :parameter3 %>
  25. </br>
  26. <%= submit_tag "Subscribe!", :class => "submit" %>
  27. </fieldset>
  28. <% end %>
  29.  
  30.  
  31. </body>
  32. </html>
  33.  
  34. Currently I have hard coded the checkbox names. Please tell me how to fetch their names from DB. Yes I am able to connect to DB and write into it. I want to use two DBs simultaneously(one for fetching checkbox names and another for the application)
  35.  
  36. Also, tell me if I need to do any changes in the site_controller code. Its code is given below:-
  37.  
  38. site_controller
  39.  
  40. class SiteController < ApplicationController
  41. require 'user_controller'
  42.  
  43.     before_filter :protect, :only => :subscribe
  44.  
  45.  
  46.   def index
  47.      @title = "Welcome to railspace!"
  48.   end
  49.  
  50.   def subscribe
  51.       @title = "Subscribe"
  52.  
  53.   end
  54.  
  55.   private
  56.   # Protect a page from unauthorized access.
  57.   def protect
  58.     unless session[:user_id]
  59.         flash[:notice] = "You don't seem to be logged in, please log in first"
  60.         redirect_to :controller => "user" , :action => "login"
  61.         return false
  62.     end
  63.   end
  64.  
  65.  
  66. end
Please help me out :)
Expert
 
Join Date: May 2007
Posts: 213
#8: Dec 11 '08

re: Ruby operator affect overall program readability


Thanks for the code, it helps me see what you are trying to accomplish. In the future, though, please try to use code tags when posting code. It makes the code more readable.

It looks like you just need to select all the checkbox fields from the database and loop through them. Maybe something like:
Expand|Select|Wrap|Line Numbers
  1. <%CheckboxModel.find(:all).each do |cb|%>
  2.   <label for="<%=cb.parameter%>"><%=cb.parameter%>:</label>
  3.   <%= form.check_box cb.parameter %>
  4. <%end%>
You could do this in either the view or controller, so you may need to do it slightly differently. Also, my experience is with Rails 1.2.3, so if you are using something different you may need to modify the code a little.
Reply