Having Trouble Deleting a Value from a Collect_Select box in Rails 3.2 -
i trying implement delete button/link on views/admin_lookups/index.html.erb
page.
i able create new value , save category list
, category collection_select box
.
can't delete work though. i've gotten routing errors template errors.
application contains admin_lookup model
, category model
here code far:
views/admin_lookups/index.html.erb
<%= form_for @admin_lookup |f| %> <div class="field"> <%= f.label(:category_id, :class => "control-label") %> <div class="controls"> <%= f.collection_select(:category_id, category.all, :id, :name, prompt: true) %> </div> </div> <% end %> <% @categories.each |category| %> <ul id="category"> <%= category.name %> </ul> <% end %> <%= form_for @category |f| %> <%= f.label :name, "new name" %><br> <%= f.text_field :name %> <%= f.submit %> **here trying implement delete link** <%= link_to 'delete', @category, remote: true, method: :delete %> <% end %>
admin_lookups_controller.rb
class adminlookupscontroller < applicationcontroller def index @admin_lookups = adminlookup.all @admin_lookup = adminlookup.new @category = category.new @categories = category.all # @category = category.destroy ....
categories_controller.rb
class categoriescontroller < applicationcontroller def create @category = category.new(params[:category]) @category.save render nothing: true end def index @categories = category.all render nothing: true end def destroy @category = category.find(params[:id]) @category.destroy render nothing: true end end
views/admin_lookups/destroy.js.erb
$("select option[value=<%= @category.id %>]").remove()
here updated code:
views/admin_lookups/index.html.erb
<%= form_for :category, method: :delete, :url => categories_path, remote: true |f| %> <div class="field"> <%= f.label(:category_id, :class => "control-label") %> <div class="controls"> <%= f.collection_select(:id, category.all, :id, :name, prompt: true) %> </div> </div> <%= f.submit "delete category value" %> <% end %> <% @categories.each |category| %> <ul id="category"> <%= category.name %> </ul> <% end %> <%= form_for @category |f| %> <%= f.label :name, "new name" %><br> <%= f.text_field :name %> <%= f.submit %> <% end %>
routes.rb
resources :categories collection delete :index, to: 'categories#destroy' end end
categories_controller.rb
class categoriescontroller < applicationcontroller def create @category = category.new(params[:category]) @category.save render nothing: true end def index @categories = category.all respond_to |format| format.js {} end end def destroy @category = if params[:id].present? category.find(params[:id]) elsif params[:category].present? category.find(params[:category][:id]) end @category.destroy end end
views/categories/destroy.js.erb
$("select option[value=<%= @category.id %>]").remove()
Comments
Post a Comment