NothingToSeeHere
NothingToSeeHere

Reputation: 2363

Can't get Update_all in a form full of check_box_tag fields to work at all in Rails 4

I've been having a serious issue with updating a list of records using a simple boolean checkbox.

I need to be able to list my "events" and check or uncheck them if the date is available. I'd like to do this on one form. I know this is possible, but I'm finally coming here after failing to get it working.

When I submit the form, it follows the correct if: redirect path as if the update had succeeded...but the the records remain unchanged and the updated_at also remains unchained.

I've tried these info sources: http://railscasts.com/episodes/52-update-through-checkboxes http://discuss.codeschool.io/t/surviving-apis-with-rails-posting-multiple-records/4776/4 http://railscasts.com/episodes/165-edit-multiple-revised

Here's where I'm at

routes.rb

resources :events do
  collection do
    put  :verified
    post :make_events
  end
end

events_controller.rb

def verified
  if Event.where(id: params[:event_ids]).update_all(available: true)
    redirect_to step2_path(:pid => @project.id,  :u => current_user.id)
  else
    redirect_to show_events_path(:pid => @project.id,  :u => current_user.id)
  end
end

show.html.erb

<%= form_tag verified_events_path(:pid =>  @project.id ), method: :put  do  %>
  <table class="table-event-dates">
    <thead>
    <tr>
      <th> </th>
      <th> </th>
    </tr>
    </thead>
    <tbody>
    <tr><% @these_events.each do |event| %>

        <td><%= check_box_tag "event_id[]", event.id, :value => event.available  %></td>
        <td><label> <%= event.date.strftime("%A, %b. %d %G") %></label></td>
        </tr>
        </tbody>
      <% end %>
      </table>

  </div><br><!-- panel-body -->
  <div class="panel-footer2">
    <div class="row">
      <%= submit_tag 'Verify Dates',  :class => 'btn btn-green btn-lg btn-block' %>
<% end %>

I've been stuck on this for a while.

There was a similar question that I posted, but the fix was not related to and did not address the error...it did, however, address a different error. I am sorry to post twice, but it seems like a different problem within my same project.

update multiple records in one form using checkboxes / wrong number of aruguments rails 4

Upvotes: 2

Views: 135

Answers (2)

NothingToSeeHere
NothingToSeeHere

Reputation: 2363

So the key to my problem turned out to be a mix of not listing the boolean variable and that I had to specify a value for my unchecked checkbox.

This saved me:

controller:

def verfied

Event.where(id: params[:event_ids]).update_all({:available => true})
Event.where.not(id: params[:event_ids]).update_all({:available => false})
return redirect_to step2_path(:pid => @project.id, :id => @project.id, :u => current_user.id, :t => @project.template_id)

else 
      redirect_to event_edit_path(:pid => project_id, :u => current_user.id)


end

Here's my final code from for the form:

 <div class="form-wrap">
          <%= form_tag verified_events_path(:pid =>  @project.id ), method: :put  do  %> 


           <table class="table-event-dates">
            <thead>
            <tr>
              <th> </th>
              <th> </th>  
            </tr>
          </thead>
           <tbody> 
            <tr><% @these_events.each do |event| %>
              <td><%= check_box_tag "event_ids[]", event.id, event.available  %><%= hidden_field_tag :updated_at,  Time.now %></td>
              <td><label> <%= event.date.strftime("%A, %b. %d %G") %></label></td>

                             </tr>
                                  </tbody>
                          <% end %>
            </table>

          </div><br><!-- panel-body -->
          <div class="panel-footer2">
            <div class="row">

                <%= submit_tag 'Verify Dates',  :class => 'btn btn-green btn-lg btn-block' %>  

            </div>   
          </div>

      </div><!-- panel -->


    </div><!-- col-md-6 --><!-- col-md-6 -->

                  <% end %> 

Upvotes: 2

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

You should change params[:event_ids] to params[:event_id]:

Event.where(id: params[:event_id]).update_all(available: true)

This works for me.

Also use logger to watch params: logger.debug { "params: #{params[:event_id]}" }. Most of the problems are in them.

Upvotes: 1

Related Questions