Rails形式的嵌套模型和多态关联

时间:2014-04-11 17:27:46

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

我有三个模型的应用程序 - Offer,OfferType和Category。一个类别有很多优惠,一个类型有很多优惠,而优惠属于一个类别和优惠类型。我正在尝试使用描述(Offer模型中的字段),forfertypes的单选按钮和使用Simple Form gem的类别下拉列表制作新表单。

目前,我已尝试基于this RailsCast实现多态关联。我想这可以让我按类别和类型来过滤优惠。我还想知道使用" accepts_nested_attributes_for"是否一个好主意。嵌套模型以使其在表单中可访问?

我的控制器出现问题,我想知道这是否仍然是解决问题的好方法。我在下面提供了我的代码。

优惠模式:

class Offer < ActiveRecord::Base
    belongs_to :offerable, polymorphic: true
end

OfferType型号:

class OfferType < ActiveRecord::Base
    has_many :offers, as: :offerable
end

优惠控制器:

class OffersController < ApplicationController
    before_action :authenticate_user!
    before_action :offer, only: [:show, :edit, :update, :destroy]
    before_filter :load_offerable

    def index
        @offer = @offerable.offers
    end

    def show
    end

    def new
        @offer = @offerable.offers.new
    end

    def edit
    end

    def create
        @offer = @offerable.offers.new(offer_params)

        respond_to do |format|
            if @offer.save
                format.html { redirect_to @offerable, notice: "Offer was created successfully." }
                format.json { render action: 'show', status: :created, location: @offer }
            else
                format.html { render action: 'new' }
                format.json { render json: @offer.errors, status: :unprocessable_entity }
            end
        end
    end

    def update
        respond_to do |format|
            if @offerable.offers.update(offer_params)
                format.html { redirect_to @offerable, notice: "Offer was created successfully." }
                format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @offerable.offers.errors, status: :unprocessable_entity }
            end
        end
    end

    def destroy
        @offerable.offers.destroy
        respond_to do |format|
            format.html { redirect_to offer_url }
            format.json { head :no_content }
        end
    end

    private

    def set_offer
        @offer = @offerable.offers.find(params[:id])
    end

    def offer_params
        params.require(:offer).permit(:description)
    end

    def load_offerable
        resource, id = request.path.split('/')[1, 2]
        @offerable = resource.singularize.classify.constantize.find(id)
    end

end

OfferTypes控制器:

class OfferTypesController < ApplicationController
    before_action :set_offer_type, only: [:show, :edit, :update, :destroy]

    def index
        @offer_type = OfferType.all
    end

    def show
    end

    def new
        @offer_type = OfferType.new
    end

    def edit
    end

    def create
        @offer_type = OfferType.new(offer_type_params)

        respond_to do |format|
            if @offer_type.save
                format.html { redirect_to @offer_type, notice: "Offer Type was successfully created." }
                format.json { render action: 'show', status: :created, location: @offer_type }
            else
                format.html { render action: 'new' }
                format.json { render json: @offer_type.errors, status: :unprocessable_entity }
            end
        end
    end

    def update
        respond_to do |format|
            if @offer_type.update(offer_type_params)
                format.html { redirect_to @offer_type, notice: "Offer Type was successfully updated." }
                format.json { head :no_content }
            else
                format.html { render action: 'edit' }
                format.json { render json: @offer_type.errors, status: :unprocessable_entity }
            end
        end
    end

    def destroy
        @offer_type.destroy
        respond_to do |format|
            format.html { redirect_to offer_types_url }
            format.json { head :no_content }
        end
    end

    private

    def set_offer_type
        @offer_type = OfferType.find(params[:id])
    end

    def offer_type_params
        params.require(:offer_type).permit(:name)
    end
end

0 个答案:

没有答案