Forum: Ruby on Rails Help with Forms

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.
Adam (Guest)
on 2006-04-19 00:32
Hello,

Ive been trying to sort out this problem for a couple of days now.

Here's my problem:

Im builing an ajax form that has a repetitive sequence of similar form
elements (tracking_id).

<%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
%>
<%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
%>
<%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
%>

Im using the :index parameter as explained here >>
http://api.rubyonrails.com/classes/ActionView/Help....

My question is: How do I find all the records in the asset table that
have the tracking_id that was entered in the text field?

In my controller Im doing this:

     params[:asset].each_value do |prod|
     	records = Asset.find_all_by_tracking_id(prod)
     end

However, this gives the error:
----------------------------------------------------------------------------
RuntimeError: ERROR	C22P02	Minvalid input syntax for integer: "---
- tracking_id
- "2202045""	Fnumutils.c	L84	Rpg_atoi: SELECT * FROM asset WHERE
(asset."tracking_id" = '---
- tracking_id
- "2202045"' )
-----------------------------------------------------------------------------

How would I loop through and find each asset record?  Any help is
greatly appreciated. As you can tell, Im pretty new at using programming
ruby.

Thanks.
Mark Reginald J. (Guest)
on 2006-04-19 01:16
(Received via mailing list)
Adam wrote:

>
> However, this gives the error:
> ----------------------------------------------------------------------------
> RuntimeError: ERROR	C22P02	Minvalid input syntax for integer: "---

records = Asset.find_all_by_tracking_id(prod[:tracking_id])

--
We develop, watch us RoR, in numbers too big to ignore.
Adam (Guest)
on 2006-04-19 01:55
Mark Reginald J. wrote:
> Adam wrote:
>
>>
>> However, this gives the error:
>> ----------------------------------------------------------------------------
>> RuntimeError: ERROR	C22P02	Minvalid input syntax for integer: "---
>
> records = Asset.find_all_by_tracking_id(prod[:tracking_id])
>
> --
> We develop, watch us RoR, in numbers too big to ignore.

That worked great!!  Thanks alot Mark.  I've been stuck on that all day.

When the form submits, Im trying to save a foreign key (medprocedure_id)
in the asset table for each of the tracking_id's in the form. Almost
have it working, however, its not saving the foreign key for all the
records.  Only for the last one.

Heres what I have:

# Get the Tracking_Ids from the form
     params[:asset].each_value do |prod|
     	@records = Asset.find_all_by_tracking_id(prod[:tracking_id])
     end

     @assets = []
     for record in @records
     @assets.insert(-1,{ 'record' => record})
     end


      # Now execute transaction and commit to db

      begin
      Asset.transaction(medproc) do

      # save new medprocedure entry
      medproc.save!

      # save link in join table (professionals)
      medproc.professionals << professional_record


     # save updated asset record
    for asset in @assets
      asset['record'].medprocedure_id = medproc.id
      asset['record'].state_id = 12
      asset['record'].save!
    end



      #raise "Exeception"
    end
Mark Reginald J. (Guest)
on 2006-04-19 02:24
(Received via mailing list)
Adam wrote:

> When the form submits, Im trying to save a foreign key (medprocedure_id)
> in the asset table for each of the tracking_id's in the form. Almost
> have it working, however, its not saving the foreign key for all the
> records.  Only for the last one.

In your original post you implied that you replicated the view line

<%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
%>

But this will not use a different value of i for each tracking_id field.
You need to do:

<% 3.times do |i| %>
<%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
%>
<% end %>

Look at your development.log file to check that the right set of
parameters are being posted.

--
We develop, watch us RoR, in numbers too big to ignore.
Adam (Guest)
on 2006-04-19 05:33
Mark Reginald J. wrote:

> But this will not use a different value of i for each tracking_id field.
> You need to do:
>
> <% 3.times do |i| %>
> <%= text_field "asset", "tracking_id", "index" => i, "class" => "case"
> %>
> <% end %>
>
> Look at your development.log file to check that the right set of
> parameters are being posted.

Mark,

I left that out intentionally.  Sorry if it was confusing.  I have the
<% #.times  do |i| %> logic in there.

When I view source in browser it looks good:

<input type="text" id="asset_0_tracking_id" name="asset[0][tracking_id]"
class="case" />
Mark Reginald J. (Guest)
on 2006-04-19 16:02
(Received via mailing list)
Adam wrote:

> I left that out intentionally.  Sorry if it was confusing.  I have the
> <% #.times  do |i| %> logic in there.

OK, your problem is that you're not accumulating your @records array
properly. Instead of

      params[:asset].each_value do |prod|
      	@records = Asset.find_all_by_tracking_id(prod[:tracking_id])
      end

if an Asset can have multiple tracking ids, you need something like:

      @records = []
      params[:asset].each_value do |prod|
      	@records += Asset.find_all_by_tracking_id(prod[:tracking_id])
      end

Also, the subsequent construction of the @assets array looks to be
superfluous.


--
We develop, watch us RoR, in numbers too big to ignore.
Adam (Guest)
on 2006-04-19 19:51
Mark Reginald J. wrote:

> OK, your problem is that you're not accumulating your @records array
> properly. Instead of
>
>       params[:asset].each_value do |prod|
>       	@records = Asset.find_all_by_tracking_id(prod[:tracking_id])
>       end
>
> if an Asset can have multiple tracking ids, you need something like:
>
>       @records = []
>       params[:asset].each_value do |prod|
>       	@records += Asset.find_all_by_tracking_id(prod[:tracking_id])
>       end
>
> Also, the subsequent construction of the @assets array looks to be
> superfluous.

Mark,

That worked perfectly.  Thanks for all your help.  I'll have to research
the += syntax.

Thanks Again!

Adam
This topic is locked and can not be replied to.