Sha256: 99b257a55a8cd6e00d8586f1c3a14ce2f4660eacb2b8b1b463f1f6b3ae01a8e6

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

class RailsDetailMy::TaxonItemsController < RailsDetailMy::BaseController
  before_action :set_taxon_item, only: [:show, :edit, :update, :destroy]

  def index
    taxon_items = TaxonItem.includes(:list).where(taxon_type: params[:taxon_type], taxon_id: params[:taxon_id])
    @items_hash = taxon_items.group_by { |i| i.list }
  end

  def show
  end

  def new
    @taxon = params[:taxon_type].safe_constantize.find params[:taxon_id]
    if params[:list_id]
      @list = List.find params[:list_id]
    else
      @lists = List.all.map { |list| [list.name, list.id] }
    end

    @taxon_item = TaxonItem.new(taxon_type: params[:taxon_type], taxon_id: params[:taxon_id], list_id: params[:list_id])
  end

  def edit
  end

  def create
    @taxon_item = TaxonItem.new(taxon_item_params)

    respond_to do |format|
      if @taxon_item.save
        format.js
        format.html { redirect_to @taxon_item, notice: 'Taxon item was successfully created.' }
      else
        format.js
        format.html { render :new }
      end
    end

  end

  def update
    respond_to do |format|
      if @taxon_item.update(taxon_item_params)
        format.js
        format.html { redirect_to @taxon_item, notice: 'Taxon item was successfully created.' }
      else
        format.js
        format.html { render :edit }
      end
    end
  end

  def destroy
    @taxon_item.destroy
  end

  private
  def set_taxon_item
    @taxon_item = TaxonItem.find(params[:id])
  end

  def taxon_item_params
    params.fetch(:taxon_item, {}).permit(:list_id, :item_id).merge(taxon_type: params[:taxon_type], taxon_id: params[:taxon_id])
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_detail-0.0.1 app/controllers/rails_detail_my/taxon_items_controller.rb