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?
Ruby operator affects overall program readability
Collapse
X
-
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? -
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
-
Issue with the code
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,
NavneetComment
-
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
-
I have created a user model, but the subscribe checkbox is with the default site controller.
Please help me out :)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 endComment
-
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:
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.Code:<%CheckboxModel.find(:all).each do |cb|%> <label for="<%=cb.parameter%>"><%=cb.parameter%>:</label> <%= form.check_box cb.parameter %> <%end%>
Comment
Comment