Topic: Working with form data

Hi, bit of a rails noob so probably made a basic error below.

I have a has_many through relationship between operatives and tasks, with the join model being taskassignments.  The taskassignments belong to an individual report.  This way each report contains the work allocation for that report date.


Anyway im working on a checkbox form to update/edit the task assignments, params is in the following format...

Parameters: {"commit"=>"update", "_method"=>"put", "id"=>"1", "taskassignment"=>{"1"=>{"operative_id"=>"1"}, "2"=>{"operative_id"=>"2"}}}

dealing with only the first task here, it sets the first taskassignment to point to the 1st operative and the second taskassignment to point to the second.

However if i were to untick the associated checkbox I get this..

Parameters: {"commit"=>"update", "_method"=>"put", "id"=>"1", "taskassignment"=>{"1"=>{"operative_id"=>"0"}, "2"=>{"operative_id"=>"2"}}}

which is fine as it indicates that the first taskassignment has been cancelled. So I need a way to process those entries where the operative_id now points to 0.  So far to update the taskassignments I have been using..

@report.taskassignments.each { |t| t.attributes = params[:taskassignment][t.id.to_s] }

which works but as i said above need  a way to process 0 operative_id's, however i cant seem to do this.  I have tried the following as a test but it doesnt seem to work...

  @report.taskassignments.each { |t| t.attributes = params[:taskassignment][t.id.to_s] }

   tas = @report.taskassignments.find(:all)
    for ta in tas
      if (ta.operative_id == 0)
        ta.operative_id = 100
      end
    end


Sorry for the long post any ideas?

Last edited by rebo (2007-01-28 12:01:28)

Re: Working with form data

Just thinking, is it not finding the 0 operative id's as the attributes have not yet been saved?

Re: Working with form data

Are you using the "check_box" method in the view? This is designed for editing a model's boolean attribute (which is why you get "0" when unchecking it). Instead you may want to use check_box_tag which will simply not send the checkbox when it isn't checked.

See this post on how to do this with a simple HABTM association. However, here you have has_many :through so it will take a little bit of extra code to get it to work.

Railscasts - Free Ruby on Rails Screencasts

Re: Working with form data

Thanks very much for the quick response much appreciated.

Yes i'm using the "check_box" helper in the view. However I require some kinda of indication that a check box has been unticked so I know to destory the respective entry in the through model.

Re: Working with form data

It appears you are looping through the taskassignments in the report in the controller so you could determine if the check-box was checked by the lack of the passed id.

@report.taskassignments.each do |taskassignment|
  if params[:taskassignment_ids].include? taskassignment.id.to_s
    # update the attributes
  else
    # remove the taskassignment
  end
end

Then again, I may not be understanding how the interface works.

Railscasts - Free Ruby on Rails Screencasts

Re: Working with form data

Ryanb can you explain how this line works...

params[:taskassignment_ids].include? taskassignment.id.to_s

As I understand it, it determines wether each task assignment owned by the report is  is referenced in the parameters POST.  I would assume one would need check_box_tag then instead of check_box so as not to send unchecked data.

Is the above correct or am I confused smile ?

The bit i dont get is how does it know what taskassignment_ids refers to as i only send taskassignment in the POST parameters.

Last edited by rebo (2007-01-28 16:14:47)

Re: Working with form data

rebo wrote:

As I understand it, it determines wether each task assignment owned by the report is  is referenced in the parameters POST.  I would assume one would need check_box_tag then instead of check_box so as not to send unchecked data.

Yes, the code I posted was for check_box_tag:

<% for taskassignment in @taskassignments %>
  <%= check_box_tag 'taskassignment_ids[]', taskassignment.id, true %>
  ....
<% end %>

Rails will collect all checked boxes into an array - this will result in params[:taskassignment_ids] returning an array of ids that were checked. Calling "include?" on this as I did before will tell you if the given taskassignment was checked.

Railscasts - Free Ruby on Rails Screencasts