I have been working on integrating AJAX into my to-do application, but I keep encountering a missing template error (
Missing template items/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}.
) on the create action. The issue seems to be related to the absence of a create.html.erb
page, as the creation of items
occurs within a lists#show
page:
<div class='new-item'>
<%= render 'items/form' %>
</div>
<div class="js-items">
<% @items.each do |item| %>
<%= div_for(item) do %>
<p><%= link_to "", list_item_path(@list, item), method: :delete, remote: true, class: 'glyphicon glyphicon-ok', style: "margin-right: 10px" %>
<%= item.name %>
<% if item.delegated_to != "" && item.user_id == current_user.id %>
<small>(Delegated to <%= item.delegated_to %>)</small>
<% elsif item.delegated_to != "" && item.user_id != current_user.id %>
<small>(Delegated by <%= item.user_id %>)</small>
<% end %></p>
<% end %>
<% end %>
</div>
Referencing the _form.html.erb
partial that it connects to:
<div class="row">
<div class="col-xs-12">
<%= form_for [@list, @item], remote: true do |f| %>
<div class="form-group col-sm-6">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter Item Name" %>
</div>
<div class="form-group col-sm-6">
<%= f.label 'Delegate To (Not Required)' %>
<%= f.text_field :delegated_to, class: 'form-control', placeholder: "Enter an Email Address" %>
</div>
<div class="text-center"><%= f.submit "Add Item to List", class: 'btn btn-primary' %></div>
<% end %>
</div>
</div>
Here is the create
method within the items_controller
:
def create
@list = List.friendly.find(params[:list_id])
@item = @list.items.new(item_params)
@item.user = current_user
@new_item = Item.new
if @item.save
flash[:notice] = "Item saved successfully."
else
flash[:alert] = "Item failed to save."
end
respond_to do |format| <<<<ERROR CALLED ON THIS LINE
format.html
format.js
end
end
Additionally, here is my create.js.erb
file:
<% if @item.valid? %>
$('.js-items').prepend("<%= escape_javascript(render(@item)) %>");
$('.new-item').html("<%= escape_javascript(render partial: 'items/form', locals: { list: @list, item: @new_item }) %>");
<% else %>
$('.flash').prepend("<div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert'>×</button><%= flash.now[:alert] %></div>");
$('.new-item').html("<%= escape_javascript(render partial: 'items/form', locals: { list: @list, item: @item }) %>");
<% end %>
I am puzzled by this error. I attempted to eliminate the format.html
line from the items_controller
, but that resulted in an unrecognized format error.