Forum: Ruby on Rails checking input data in the form when button clicked..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jay P. (Guest)
on 2008-10-06 15:28
Hi, I'm trying to check the data in the fields in the form and if valid
save in the corresponding tables in the database. But (as I'm new to
rails) just couldn't figure out how I can do it. Here's how I'm trying
just to check the input data:

IN CONTROLLER:
--------------------------------------------------------------------------------
class UserController < ApplicationController
  def index
  end
  def update_campsites
    @campholder=[]
    id=params["walk_id"]
    @camps=Campsite.find_by_sql("select camp_location from campsites
where walk_id="+id)
    for eachcamp in @camps
      @campholder << [eachcamp.camp_location, eachcamp.camp_location]
    end
    if request.post?
      @arrival=params[:arrival_date]
      @nights=params[:nights]
      @children=params[:children]
      print @camp
      print @arrival
      print @nights
      print @children
    end
  end
end
--------------------------------------------------------------------------------

IN VIEW: (index.rhtml)
--------------------------------------------------------------------------------
<h1>Great Walks Online Booking</h1>
<form method="get">
  <%= select(:selected, :id, GreatWalk.getAvailableLocations, {:prompt
=> 'Select a category'}, :id => :id_selected) %>

  <%= observe_field :id_selected, :url => {:action =>
:update_campsites}, :update => :campsites, :with => "walk_id" %>
</form>
<%form_tag(:action=>:update_campsites) do %>
  <div id="campsites">
    <p>
      <label for="campgrounds">Campgrounds</label>
      <%=select "selected", "camp", Campsite.getAvailableCamps%>
    </p>
  </div>
  <p>
    <label for="arrival date">Arrival Date</label>
    <%=date_select :arrival_date, params[:arrival_date]%>
  </p>
  <p>
    <label for="nights">Nights</label>
    <%=text_field_tag :nights, params[:nights]%>
  </p>
  <p>
    <label for="parents">Parents</label>
    <%=text_field_tag :parents, params[:parents]%>
  </p>
  <p>
    <label for="children">Children</label>
    <%=text_field_tag :children, params[:children]%>
  </p>
  <p>
    <label for="total">Total</label>
    <%=text_field_tag :total, params[:total]%>
  </p>
  <%=submit_tag "Calculate"%>
<%end%>
--------------------------------------------------------------------------------

IN VIEW: (update_campsites.rhtml)
--------------------------------------------------------------------------------
<%=select "selected","camp",@campholder%>
--------------------------------------------------------------------------------

I'm getting this error:
================================================================================
TypeError in UserController#update_campsites
can't convert nil into String
RAILS_ROOT: C:/Users/Jay/Aptana/greatwalks

Application Trace | Framework Trace | Full Trace

Request
Parameters:

{"authenticity_token"=>"7ab6ae4172e01c7cb2e7346ee13ce55548da1a7a",
 "selected"=>{"camp"=>"Jabiru Swamp"},
 "arrival_date"=>{"(1i)"=>"2008",
 "(2i)"=>"10",
 "(3i)"=>"6"},
 "nights"=>"df",
 "parents"=>"klj",
 "children"=>"kj",
 "total"=>"kj",
 "commit"=>"Calculate"}

Show session dump

Response
Headers:

{"Cache-Control"=>"no-cache",
 "cookie"=>[]}
================================================================================
This topic is locked and can not be replied to.