Ruby - 显示相关业务

时间:2014-03-31 18:28:19

标签: ruby-on-rails-4

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

我想将其限制在三个相关的业务中。

谢谢!

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 个答案:

答案 0 :(得分:0)

更新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除了当前业务属于同一类别的当前业务之外,将具有&lt; = 3 businesses。然后,您可以轻松地在@others视图中使用show