Forum: Ruby on Rails Checkboxes - Saving a Checked record to another table

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-25 00:19
I am displaying a bunch of records from a table called "assets".  Next
to each record i have a checkbox.  When this box is checked i want to be
able to save the id of the record into another table called
"flagged_assets". All I need is the ID, nothing else.  Im struggling
with the logic to save to another table.  I know how to do it if the
asset_id is already in the "flagged_assets" table.

# when flag_assets page is loaded
def flag_assets
   @assets = Asset.find :all
  end

# Called when Ajax form is submitted
  def get_flagged

   to_flag = params[:to_be_flagged]

     if to_flag
       to_flag.each do |asset_id, flag|
    	 @assets = FlaggedAsset.find_all_by_asset_id(asset_id)
    	   for asset in @assets
    	    if flag == "yes"
    	     asset.flag = 1
    	    else
    	     asset.flag = 0
    	    end
    	   asset.save!
         end
      end
   end
   render :text => "<p class='success'>Assets have been Flagged</p>"
  end

  def get_flagged

   to_flag = params[:to_be_flagged]

     if to_flag
       to_flag.each do |asset_id, flag|
    	 @assets = Test.find_all_by_asset_id(asset_id)
    	   for asset in @assets
    	    if flag == "yes"
    	     asset.flag = 1
    	    else
    	     asset.flag = 0
    	    end
    	   asset.save!
         end
      end
   end
   render :text => "<p class='success'>Assets have been Flagged</p>"
  end
Adam (Guest)
on 2006-04-25 00:21
Please disregard the second def get_flagged method.  Pasted by accident.
Bob (Guest)
on 2006-04-25 00:55
Adam wrote:
> I am displaying a bunch of records from a table called "assets".  Next
> to each record i have a checkbox.  When this box is checked i want to be
> able to save the id of the record into another table called
> "flagged_assets". All I need is the ID, nothing else.  Im struggling
> with the logic to save to another table.  I know how to do it if the
> asset_id is already in the "flagged_assets" table.
>
> # when flag_assets page is loaded
> def flag_assets
>    @assets = Asset.find :all
>   end
>
> # Called when Ajax form is submitted
>   def get_flagged
>
>    to_flag = params[:to_be_flagged]
>
>      if to_flag
>        to_flag.each do |asset_id, flag|
>     	 @assets = FlaggedAsset.find_all_by_asset_id(asset_id)
>     	   for asset in @assets
>     	    if flag == "yes"
>     	     asset.flag = 1
>     	    else
>     	     asset.flag = 0
>     	    end
>     	   asset.save!
>          end
>       end
>    end
>    render :text => "<p class='success'>Assets have been Flagged</p>"
>   end
>
>   def get_flagged
>
>    to_flag = params[:to_be_flagged]
>
>      if to_flag
>        to_flag.each do |asset_id, flag|
>     	 @assets = Test.find_all_by_asset_id(asset_id)
>     	   for asset in @assets
>     	    if flag == "yes"
>     	     asset.flag = 1
>     	    else
>     	     asset.flag = 0
>     	    end
>     	   asset.save!
>          end
>       end
>    end
>    render :text => "<p class='success'>Assets have been Flagged</p>"
>   end
Hi Adam,

From what I understand you want to only store the ID of those that are
checked into a new table.  In this case why donĂ¢??t you iterate through
all the checked boxes in the hash and create a new FlaggedAsset object
with the ID for each iteration?

By the way, why donĂ¢??t you put a flag column in the Asset table, instead
of creating a new table?

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