Klairvoyant infosolution's Blog

Forms in rails, different ways

john

1) Method 1

a) input.html.erb

<form action=”/look/at”>
<input type=”text” name=’text1′>
<input type=”checkbox” name=”checkbox1″>
<input type=”radio” name=”radio1″ value=”red”>Red
<input type=”radio” name=”radio1″ value=”green”>Green
<input type=”radio” name=”radio1″ value=”blue”>Blue
<select name=”select1″>
<option value=’green’/> Green
<option value=’Blue’/> Blue
<option value=’Red’/> Red
<option value=’Orange’/> Orange
</select>
<select name=”select2[]” multiple size=”3″>
<option value=’AC’/> AC
<option value=’Music system’/> Music Syatem
<option value=’Heater’/> Heater
<option value=’auto transmission’/> Auto Transmision
</select>
<input type=”submit”>
</form>

b) look_controller.rb

def at
#@data=params[:text1]
#@data2=params[:checkbox1]
#@data3=params[:radio1]
#@data4=params[:select1]
#@data5=params[:select2]
end

c) at.html.erb

<body>
Entered name is <%=@data%>
<br>
<%if @data2 %>
<br> Check box Ticked
<%else %>
checkbox not ticked
<% end%>
<br>you checked <%= @data3%>
<br>
you selected <%= @data4%>
<br> you selected
<% for data in @data5 %>
<%= data%>
,
<% end %>
</body>

2) Method 2 (short cut) way

a) input.html.erb

<% form_tag ‘at’,:method => :get do -%>

<%= text_field_tag ‘text1′%>

<%= check_box_tag ‘checkbox1′ %>

Red<%= radio_button_tag ‘radio1′,’red’%>
Green<%= radio_button_tag ‘radio1′,’green’%>
blue<%= radio_button_tag ‘radio1′,’blue’%>

<%= select_tag ‘select1′,’<option>green</option><option> blue </option><option>red</option><option>orange</option>’ %>

<%= select_tag ‘select2[]‘, ‘<option>AC</option><option>Music  System</option> <option>heater </option> <option>auto transmission</option>’, :multiple=>true %>

<%= submit_tag%>
<% end -%>

b) look_controller.rb

def at
#@data=params[:text1]
#@data2=params[:checkbox1]
#@data3=params[:radio1]
#@data4=params[:select1]
#@data5=params[:select2]
end

c) at.html.erb

<body>
Entered name is <%=@data%>
<br>
<%if @data2 %>
<br> Check box Ticked
<%else %>
checkbox not ticked
<% end%>
<br>you checked <%= @data3%>
<br>
you selected <%= @data4%>
<br> you selected
<% for data in @data5 %>
<%= data%>
,
<% end %>
</body>

3) Method 3 – storing in models

a) input.html.erb

<% form_tag ‘at’,:method => :get do -%>
<%= text_field(“cruncher”, “text1″, {“size” => 30})%>
<%= check_box(“cruncher”,’checkbox1′) %>
Red<%= radio_button (‘cruncher’,’radio1′,’red’)%>
Green<%= radio_button (‘cruncher’,’radio1′,’green’)%>
blue<%= radio_button (‘cruncher’,’radio1′,’blue’)%>
<%=select(“cruncher”, “select1″, {‘red’ => ‘red’, ‘green’ => ‘green’, ‘blue’ => ‘blue’}) %>
<%= select(“cruncher”,”select2″,{‘AC’ => ‘AC’, ‘Music’ => ‘Music’,’heater’ => ‘heater’,’auto transmission’=>’auto transmission’}, {}, {:multiple => true, :size => 3}) %>
<%= submit_tag%>
<% end -%>

b) look_controller.rb

def at
@data_hash = params[:cruncher]
@data=@data_hash[:text1]
@data2=@data_hash[:checkbox1]
@data3=@data_hash[:radio1]
@data4=@data_hash[:select1]
@data5=@data_hash[:select2]
end

c) at.html.erb

<body>
Entered name is <%=@data%>
<br>
<%if @data2 %>
<br> Check box Ticked
<%else %>
checkbox not ticked
<% end%>
<br>you checked <%= @data3%>
<br>
you selected <%= @data4%>
<br> you selected
<% for data in @data5 %>
<%= data%>
<% end %>
</body>

4) Method 4

<%= form_tag(gift_receivers_path, :method => “get”) do %>
<%= label_tag(:catagory, “Catagory:”) %>
<%= select_tag(:catagory,options_for_select([['All'],['Apparel'],['Appliances'],['ArtsAndCrafts'],['Automotive'],['Baby'],['Beauty'],['Blended'],['Books'],['Classical'],['DigitalMusic'],['DVD'],
['Electronics'],['GourmetFood'],['Grocery'],['HealthPersonalCare'],['HomeGarden'],['Industrial'],['Jewelry'],['KindleStore'],['Kitchen'],['Magazines'],['Marketplace'],['Miscellaneous'],['MobileApps'],['MP3Downloads'],['Music'],['MusicalInstruments'],['MusicTracks'],['OfficeProducts'],['OutdoorLiving'],['PCHardware'],['PetSupplies'],['Photo'],['Shoes'],['Software'],['SportingGoods'],['Tools'],['Toys'],['UnboxVideo'],['VHS'],['Video'],['VideoGames'],['Watches'],['Wireless'],['WirelessAccessories']])) %>
<%= label_tag(:q, “Search for:”) %>
<%= text_field_tag(:q) %>
<%= submit_tag(“Search”) %>
<% end %>

<%= select_tag(:catagory,options_for_select([['All',1],['Apparel',2],['Appliances',3],..])) %> also possible   then  on controller params[:catagory]  value will be corresponding number.

on controller

key2=params[:catagory]