无法从Rails 3.2中的Collect_Select框中删除值

时间:2014-09-19 20:13:27

标签: ruby-on-rails ruby-on-rails-3

我正在尝试在views/admin_lookups/index.html.erb页面上实施删除按钮/链接。

我可以创建新值并将其保存到category listcategory collection_select box

虽然无法删除。我已经收到了从路由错误到模板错误的所有内容。

应用包含admin_lookup modelcategory model

到目前为止,这是我的代码:

视图/ admin_lookups / index.html.erb

<%= form_for @admin_lookup do |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 do |category| %> 
  <ul id="category">
    <%= category.name %>
  </ul>   
<% end %>

<%= form_for @category do |f| %>
<%= f.label :name, "New Name" %><br>
<%= f.text_field :name %>
<%= f.submit %>

 **Here is where I am trying to implement my 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

查看/ admin_lookups / destroy.js.erb

 $("select option[value=<%= @category.id %>]").remove()

1 个答案:

答案 0 :(得分:0)

这是我更新的代码:

<强>视图/ admin_lookups / index.html.erb

<%= form_for :category, method: :delete, :url => categories_path, remote: true do |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 do |category| %> 
  <ul id="category">
    <%= category.name %>
  </ul>   
<% end %>

<%= form_for @category do |f| %>
<%= f.label :name, "New Name" %><br>
<%= f.text_field :name %>
<%= f.submit %>

<% end %>

<强>的routes.rb

resources :categories do
  collection do 
    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 do |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

<强>查看/分类/ destroy.js.erb

 $("select option[value=<%= @category.id %>]").remove()