Rails:复选框可在点击

时间:2016-06-07 22:50:39

标签: javascript jquery ruby-on-rails

我有一个基本的应用类型应用,我试图将completed的变量item更改为当我点击复选框时的当前日期/时间。我找到了this post,这使我得到了以下代码结构:

我的lists#show,其中items已创建,已检查(更新),已编辑和已删除:

<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 -->

以下是我update中的items_controller方法(我认为它具有管辖权而不是lists_controller,即使它是lists#show页面,因为它是item 1}}正在更新:

  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

以下是目前正在出现的内容:

<code>lists#show</code> view

以下是我遇到的两个问题:

  1. 轻微的重要问题:复选框应与列表项一起显示,但不是。我的班级inline-block只是在application.scss文件中添加了这个链接:

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

  2. 更重要的问题:即使点击了该复选框,completed的值仍然是nil,因为我的控制台:

    [6] pry(main)&gt; Item.where(名称:“Feed Dexter”)   项目加载(0.1ms)SELECT“items”。* FROM“items”WHERE“items”。“name”=? [[“name”,“Feed Dexter”]] =&GT; [#]

  3. 任何想法如何使这个功能?

0 个答案:

没有答案