0

I have a basic to do type app and I am trying to change the variable completed for an item to the current date/time when I click a checkbox. I found this post, which has led me to the following code structure:

My lists#show, on which items are created, checked off (updated), edited, and deleted:

<div class="container">
  <div class="row" style="height: 70px"></div>
</div>

<div class="col-xs-10 col-xs-push-1 container">
  <div class="md-well">
    <h1 class="text-center"><%= @list.name %></h1>
    <% if @items.count == 0 %>
      <h4 class="text-center">You don't have any items on this list yet! Want to add some?<h4>
    <% elsif @items.count == 1 %>
      <h4 class="text-center">Only <%= @items.count %> Item To Go!</h4>
    <% else %>
      <h4 class="text-center">You Have <%= @items.count %> Items To Go!</h4>
    <% end %>

    <%= render 'items/form' %>

    <% @items.each do |item|%>
      <p>
       <%= form_for [@list, item], class: "inline-block", id: 'item<%= item.id %>' do |f| %>
         <%= f.check_box :completed, :onclick => "$this.parent.submit()" %>
       <% end %>
        <%= item.name %>
        ( <%= link_to "Delete", list_item_path(@list, item), method: :delete %> )
      </p>
    <% end %>

    <div class="text-center">
      <%= link_to "Back to My Lists", lists_path %>
    </div> <!-- text-center -->

  </div> <!-- well -->
</div> <!-- container -->

Here is my update method in my items_controller (which I believe has jurisdiction instead of the lists_controller even though it is the lists#show page because it is an item being updated:

  def update
    @list = List.friendly.find(params[:list_id])
    @item = @list.items.find(params[:id])

    @item.name = params[:item][:name]
    @item.delegated_to = params[:item][:delegated_to]
    @item.days_til_expire = params[:item][:days_til_expire]
    @item.completed = params[:item][:completed]
    @item.user = current_user

    if @item.update_attributes(params[:item])
      @item.completed = Time.now
    end

    if @item.save
      flash[:notice] = "List was updated successfully."
      redirect_to @list
    else
      flash.now[:alert] = "Error saving list. Please try again."
      render :edit
    end
  end

Here is what is currently appearing:

<code>lists#show</code> view

And here are the two problems I'm having:

  1. LESS IMPORTANT PROBLEM: The checkboxes should be displayed inline with the list items but aren't. My class inline-block simply makes links to this in the application.scss file:

    .inline-block { display: inline-block !important; }

  2. MORE IMPORTANT PROBLEM: Even after the checkbox is clicked, the value for completed remains nil as per my console:

    [6] pry(main)> Item.where(name: "Feed Dexter") Item Load (0.1ms) SELECT "items".* FROM "items" WHERE "items"."name" = ? [["name", "Feed Dexter"]] => [#]

Any ideas how to make this functional?

9
  • Why do you have a update_attributes followed by a save in the same flow of action? Commented Jun 7, 2016 at 23:22
  • The update_attributes is a recent addition (while trying to implement the checkbox functionality) and the save was working prior to this change. The lack of functionality persists when both are combined under the update_attributes, removing the save. Should they be combined anyway? Commented Jun 7, 2016 at 23:27
  • id: "item" - if every form has the same id... html will be confused as to which form to submit when you do this: "$('#item').submit()" - it will pick the first one out of however many are on the page to submit. Make sure you give each form a unique id eg try: id: "item<%= item.id %>" "$('#item<%= item.id %>').submit()" (note, not tested it will probably have bugs that you can fix with google). Though possibly you could do better with something like "$this.parent.submit()" or similar Commented Jun 7, 2016 at 23:29
  • update_attributes does the same thing as save. Only in the first you pass an hash, and in the second you call the method from an object. So basically, it is the same thing. You can remove the update_attributes call and just use save. Commented Jun 7, 2016 at 23:29
  • 1
    @TarynEast, well you said "it will probably have bugs", and I don't speak javascript very well, so this could take some time. crosses fingers and hopes javascript ninja falls from the sky Commented Jun 7, 2016 at 23:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.