Generating Input Fields Using ERB

Hello Everyone

While making a signup page for a simple web application I am supposed to
use check boxes, radio buttons,text boxes ,etc. but have failed to do
so.
Though I have successfully generated a text field by using the following
code.

  • <%= form_for(@user) do |f| %>*

  • <%= render ‘shared/error_messages’ %>*

  • *
  •  <% flash.each do |key, value| %>*
    
  •    <div class="alert alert-<%= key %>"><%= value %></div>*
    
  •  <% end %>*
    
  • *
  •  <%= f.label :fname,"First Name" %>*
    
  •  <%= f.text_field :fname,:value =>
    

lcurrent_user[‘firstName’],:class=>“form-control” %>*

The above code generates a text field for first name

Please suggest how to achieve the same for checkboxes, radio buttons,
text-boxes,etc.

PS- neglect if any tags are not closed.
Regards
Varun Wachaspati J
B.E.(Hons.)Computer Science | LinkedIn
https://mailtrack.io/trace/link/686bfc7ecd2b18f7473f9c5d13aab20aba51195b
|
+91-8094068111

Birla Institute of Technology & Science, Pilani

Hey John
Thanks for insights, they were really helpful
But I am having a hard time getting the data from the checkboxes into
the
database.
Have a look at the code -

  •  <%= f.label :expertise,"Kindly select your field of expertise" 
    

%>*

  •  <%= f.check_box :expertise %>*
    
  •  <%= label_tag(:expertise, "Fundraising") %>*
    
  •  <%= check_box_tag(:expertise) %>*
    
  •  <%= label_tag(:expertise, "Enhancing Sales") %>*
    

For normal text fields this is the code-

  •  <%= f.label :fname,"First Name" %>*
    
  •  <%= f.text_field :fname,:class=>"form-control" %>*
    

This works perfectly fine.

Thanks in advance
Regards

Varun Wachaspati J
B.E.(Hons.)Computer Science | LinkedIn
https://www.linkedin.com/profile/view?id=262723055&authType=name&authToken=XtkG&trk=api*a109924*s118458*
|
+91-8094068111

Birla Institute of Technology & Science, Pilani

form_for is one of the most quintessential ways to do forms in rails,
and
so it therefore has extensive examples provided in the documentation. A
quick google for “rails form_for” turned up these guys with examples of
all
the things you mentioned, even in erb:

Note though that, ideally, you want the @user variable to dictate the
values of the fields. In your example you have it explicitly drawing the
value out of lcurrent_user[‘firstName’]. Maybe there’s a way to adjust
@user so it already has that value assigned to fname so that the form
helper can automatically draw it out? Best to squeeze as much behavior
out
of the view as you can.

Might be getting a bit too rails specific though shrugs

On Sat Dec 20 2014 at 4:36:53 AM Varun Wachaspati
[email protected]

Research rails form helpers.

 On Saturday, December 20, 2014 7:36 AM, Varun Wachaspati 

[email protected] wrote:

Hello Everyone

While making a signup page for a simple web application I am supposed to
use check boxes, radio buttons,text boxes ,etc. but have failed to do
so.
Though I have successfully generated a text field by using the following
code.

<%= form_for(@user) do |f| %>    <%= render 'shared/error_messages' 

%>

<% flash.each do |key, value| %>

<%= value %>
<% end %>
<%= f.label :fname,"First Name" %> <%= f.text_field :fname,:value => lcurrent_user['firstName'],:class=>"form-control" %> The above code generates a text field for first name

Please suggest how to achieve the same for checkboxes, radio buttons,
text-boxes,etc.

PS- neglect if any tags are not closed.RegardsVarun Wachaspati
JB.E.(Hons.)Computer Science | LinkedIn | +91-8094068111Birla Institute
of Technology & Science, Pilani

Did you check what parameters are getting sent when you submit? Also it
looks like you have multiple checkboxes identifying themselves
similarly.

If you’re going with the form_for approach, then you’ll want to try to
make
all of your fields (text, checkbox, whatever) use the f.check_box
version
you used on the second line, and not the check_box_tag version you used
on
the fourth line. Also it seems like you have 3 labels for only 2
fields…
Maybe the first one isn’t supposed to be a label? Labels are associated
with exactly one input and clicking on a label highlights/toggles that
input, so you probably want the first line to be a simple div or
something.
Also, might radio buttons be better here? The wording “select your
field”
suggests it’s a mutually exclusive choice.

I’d suggest trying to follow a tutorial on getting a basic rails form
going
(or watching a video tutorial, there are many covering this topic) or
play
with an existing application that uses forms so you can get a feel for
how
the working version works. It can be a little confusing trying to debug
when you’re starting from scratch and don’t have a clear baseline
because
you have to connect everything up just right before it does much at all
:stuck_out_tongue:
Once you get the hang of it, it’s very easy, but can be
confusing/frustrating at first so take the easiest route to start, but I
guess if your one field is already working then you’re probably close.

On Sat Dec 20 2014 at 10:01:08 AM Varun Wachaspati <