Ruby operator affects overall program readability

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navneet7
    New Member
    • Nov 2008
    • 1

    Ruby operator affects overall program readability

    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?
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    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?

    Comment

    • itcoll
      New Member
      • Nov 2008
      • 3

      #3
      mysql_fetch_arr ay 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 .

      Comment

      • improvcornartist
        Recognized Expert Contributor
        • May 2007
        • 303

        #4
        Originally posted by itcoll
        mysql_fetch_arr ay 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.

        Comment

        • navneet023
          New Member
          • Nov 2008
          • 2

          #5
          Issue with the code

          Originally posted by improvcornartis t
          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

          Comment

          • improvcornartist
            Recognized Expert Contributor
            • May 2007
            • 303

            #6
            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?

            Comment

            • navneet023
              New Member
              • Nov 2008
              • 2

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

              Code:
              view
              subscribe.rhtml
              
              <html>
              <head>
              <title>Subscribe</title>
              </head>
              <body>
              <h2>Subscribe</h2>
              
              <% form_for :user do |form| %>
              
              <fieldset>
              <legend>Enter Your Details</legend>
              
              
              <label for="%parameter1">Parameter1:</label>
              <%= form.check_box :parameter1 %>
              </br>
              <label for="%parameter2">Parameter2:</label>
              <%= form.check_box :parameter2 %>
              </br>
              <label for="%parameter3">Parameter3:</label>
              <%= form.check_box :parameter3 %>
              </br>
              <%= submit_tag "Subscribe!", :class => "submit" %>
              </fieldset>
              <% end %>
              
              
              </body>
              </html>
              
              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)
              
              Also, tell me if I need to do any changes in the site_controller code. Its code is given below:-
              
              site_controller
              
              class SiteController < ApplicationController
              require 'user_controller'
              
                  before_filter :protect, :only => :subscribe
              
              
                def index
                   @title = "Welcome to railspace!"
                end
              
                def subscribe
                    @title = "Subscribe"
                    
                end
              
                private
                # Protect a page from unauthorized access.
                def protect
                  unless session[:user_id]
                      flash[:notice] = "You don't seem to be logged in, please log in first"
                      redirect_to :controller => "user" , :action => "login"
                      return false
                  end
                end
              
              
              end
              Please help me out :)
              Last edited by eWish; Dec 13 '08, 02:50 AM. Reason: Added Code Tags

              Comment

              • improvcornartist
                Recognized Expert Contributor
                • May 2007
                • 303

                #8
                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:
                Code:
                <%CheckboxModel.find(:all).each do |cb|%>
                  <label for="<%=cb.parameter%>"><%=cb.parameter%>:</label>
                  <%= form.check_box cb.parameter %>
                <%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.

                Comment

                Working...