代码之家  ›  专栏  ›  技术社区  ›  user1724295

Ruby相关业务

  •  0
  • user1724295  · 技术社区  · 12 年前

    我的每一项业务都属于一个类别,所以我试图找出如何展示相关业务。

    我想把它限制在三个相关的业务上。

    非常感谢。

    class BusinessesController < ApplicationController
      respond_to :html, :xml, :json
    
      before_filter :restrict_access, :only => [:edit, :update]
    
      def index
        @businesses = Business.all
        respond_with(@businesses)
      end
    
      def show
        @business = Business.friendly.find(params[:id])
        if request.path != business_path(@business)
            redirect_to @business, status: :moved_permanently
        end
      end
    
      def new
        @business = Business.new
        3.times { @business.assets.build }
        respond_with(@business)
      end
    
      def edit
        @business = get_business(business_params)
        @avatar = @business.assets.count
        @avatar = 3-@avatar
        @avatar.times {@business.assets.build}
      end
    
      def create
        @business = Business.new(business_params)
        if @business.save
          redirect_to @business, notice: 'Business was successfully created.'
        else
          3.times { @business.assets.build }
          render 'new'
        end
    end
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   Kirti Thorat    12 年前

    更新 show 行动如下:

      def show
        @business = Business.friendly.find(params[:id])
        @others = @business.category.businesses.where.not(id: @business.id).limit(3)
        if request.path != business_path(@business)
            redirect_to @business, status: :moved_permanently
        end
      end
    

    @others 将具有<=3. businesses 而不是属于当前业务的同一类别的当前业务。然后可以使用 @其他 在 显示 轻松查看。