シンプルな買い物リストアプリです。左列の div はリストのリストです。特定のリストのアイテムは、右側の列の div に表示されます。
プロセスは次のとおりです。
ユーザーがリストの 1 つをクリックすると、右側の div に関連するアイテムが (AJAX) 読み込まれます。これはうまくいっています。
これが私が立ち往生している場所です:
アイテムの上には、下のアイテムのリストに新しいアイテムを送信するための入力フィールドがあります。AJAX で新しいアイテムを下のアイテムに追加したいのですが、フォーム フィールドで Enter キーを押しても何も起こりません。更新すると、新しいアイテムが追加されます。
サーバーが言ったことは次のとおりです。
Processing by ItemsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"YWIlMPXqoTvhqzGGPqm6Rn/E7jPRt8do/2tkjd0H1Qk=", "item"=>{"name"=>"Mayo"}, "list_id"=>"4"}
List Load (0.1ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = ? LIMIT 1 [["id", "4"]]
(0.0ms) begin transaction
SQL (20.9ms) INSERT INTO "items" ("completed", "created_at", "description", "list_id", "name", "position", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["completed", false], ["created_at", Mon, 29 Apr 2013 21:59:06 UTC +00:00], ["description", nil], ["list_id", 4], ["name", "Mayo"], ["position", nil], ["updated_at", Mon, 29 Apr 2013 21:59:06 UTC +00:00]]
(1.6ms) commit transaction
Rendered items/_piece.html.erb (0.0ms)
Rendered items/create.js.erb (0.7ms)
ページの読み込みプロセスは次のとおりです。
home.html.erb
> _single.html.erb
> >にcreate.js.erb
追加_piece.html.erb
home.html.erb
items_controller.rb
これは私のファイルからの作成アクションです:
def create
@list = List.find(params[:list_id])
@item = @list.items.create!(params[:item])
respond_to do |format|
format.html
format.js
end
end
これは私のcreate.js
ファイルです:
$('#item-container').prepend('<%= j render(partial: 'piece', locals: {item: @item}) %>');
これは私の _piece.html.erb ファイルです:
<li><%= item.name %></l1>
これは私の_single.html.erb
ファイルです:
<div id="filter">
<div id="filter-left">
</div>
<div id="filter-right">
<%= link_to 'Delete', @list, :method => :delete %>
</div>
</div>
<div id="quick-add">
<%= form_for [@list, @item], :id => "form-quick", remote: true do |form| %>
<%= form.text_field :name, :id => "input-quick", :autocomplete => "off" %>
<% end %>
</div>
<ul class="item-container">
<% @list.items.incomplete.each do |item| %>
<% if item.id %>
<li><%= link_to "", complete_item_path(@list.id,item.id), :class => "button-item button-item-incomplete" %> <%= item.name %></li>
<% end %>
<% end %>
</ul>
<div class="title">Completed items</div>
<ul class="item-container">
<% @list.items.completed.each do |item| %>
<% if item.id %>
<li><div class="button-item button-item-completed"></div><%= item.name %></li>
<% end %>
<% end %>
</ul>
これは私のhome.html.erb
ファイルです:
<div id="main-left">
<div id="main-left-1">
<%= link_to 'Add List', new_list_path, class: "button", id: "new_link", remote: true %>
</div>
<ul id="lists" data-update-url="<%= sort_lists_url %>">
<% @lists.each do |list| %>
<%= content_tag_for :li, list do %>
<%= link_to list.name, list, :remote => true, :class => "link" %>
<% end %>
<% end %>
</ul>
</div>
<div id="main-right">
</div>