473,492 Members | 4,279 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ruby operator affects overall program readability

1 New Member
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?
Nov 21 '08 #1
7 3457
improvcornartist
303 Recognized Expert Contributor
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?
Nov 21 '08 #2
itcoll
3 New Member
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 .
Nov 28 '08 #3
improvcornartist
303 Recognized Expert Contributor
@itcoll
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.
Nov 29 '08 #4
navneet023
2 New Member
@improvcornartist
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
Dec 10 '08 #5
improvcornartist
303 Recognized Expert Contributor
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?
Dec 10 '08 #6
navneet023
2 New Member
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 :)
Dec 11 '08 #7
improvcornartist
303 Recognized Expert Contributor
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.
Dec 11 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

220
18799
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
6497
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
13
2681
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
65
5452
by: Amol Vaidya | last post by:
Hi. I am interested in learning a new programming language, and have been debating whether to learn Ruby or Python. How do these compare and contrast with one another, and what advantages does one...
16
2470
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
26
2682
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
39
4673
by: dancer | last post by:
Can somebody tell me why I get this message with the following code? Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and 'System.Web.UI.WebControls.TextBox'. ...
2
7319
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
9
2280
by: Erwin Moller | last post by:
Hi Group, This may seem a odd question in a PHP group, but I think this might be a good place to ask since I am mainly a PHP coder these days that maybe starts with Ruby. Situation: A client...
1
2803
by: Ray99 | last post by:
How does Ruby operator affect overall program readability?
0
7118
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.