我有一个基本的应用类型应用,我试图将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
以下是目前正在出现的内容:
以下是我遇到的两个问题:
轻微的重要问题:复选框应与列表项一起显示,但不是。我的班级inline-block
只是在application.scss
文件中添加了这个链接:
.inline-block { display:inline-block!important; }
更重要的问题:即使点击了该复选框,completed
的值仍然是nil
,因为我的控制台:
[6] pry(main)&gt; Item.where(名称:“Feed Dexter”) 项目加载(0.1ms)SELECT“items”。* FROM“items”WHERE“items”。“name”=? [[“name”,“Feed Dexter”]] =&GT; [#]
任何想法如何使这个功能?