用户能够看到所有者属性Rails 5.2

问题描述 投票:0回答:1

我一直在开发类似Real Estate平台的应用程序。但是我在特定功能上遇到了麻烦。

我有两种类型的用户:所有者用户。所有者创建属性,用户显然需要可视化它们并与之交互。这两个用户都是使用devise开发的,所有者可以成功创建属性,但是 用户看不到它们

因此,当我以User身份登录时,无论显示的顺序如何,我都希望看到所有者已创建的一些属性。我遇到这个问题是因为我需要创建一个过滤器,以便Users可以找到特定的属性,但是如果用户看不到属性,则不能进行任何过滤。

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

Create_properties迁移

class CreateProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :properties do |t|
      t.string :name
      t.text :description
      t.integer :price
      t.string :address
      t.float :latitude
      t.float :longitude



      t.references :owner
      t.timestamps
    end
    add_index :properties, [:id, :created_at]
  end
end

Properties_controller.rb

class PropertiesController < ApplicationController
  before_action :set_property, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_owner!

  # GET /properties
  # GET /properties.json
  def index
    @properties = Property.all
  end

  # GET /properties/1
  # GET /properties/1.json
  def show

  end

  # GET /properties/new
  def new
    @property = current_owner.properties.build
    @property.amenities.build
    @property.services.build
    @property.build_propertytype

  end

  # GET /properties/1/edit
  def edit
  end

  # POST /properties
  # POST /properties.json
  def create
    @property = current_owner.properties.build(property_params)

    respond_to do |format|
      if @property.save
        format.html { redirect_to @property, notice: 'Tu propiedad ha sido creada!' }
        format.json { render :show, status: :created, location: @property }
      else
        format.html { render :new }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /properties/1
  # PATCH/PUT /properties/1.json
  def update
    respond_to do |format|
      if @property.update(property_params)
        format.html { redirect_to @property, notice: 'La propiedad ha sido actualizada.' }
        format.json { render :show, status: :ok, location: @property }
      else
        format.html { render :edit }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /properties/1
  # DELETE /properties/1.json
  def destroy
    @property.destroy
    respond_to do |format|
      format.html { redirect_to properties_url, notice: 'La propiedad ha sido eliminada!' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_property
      @property = Property.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def property_params
      params.require(:property).permit(:id, :description, :price, :sharable, :address, { pictures: [] }, amenities_attributes: [:id, :bathroom, :room, :pool, :gym, :kitchen, :terrace, :balcony, :living_room, :garage, :parking_lot, :green_areas], propertytype_attributes: [ :apartment, :building, :office, :local_comercial, :house, :terrain ], services_attributes: [:wifi, :electricity, :gas, :guard, :air_conditioning, :water, :include_furniture])
    end
end

属性模型

class Property < ApplicationRecord
    belongs_to :owner
    has_many :amenities
    has_many :services
    has_one :propertytype

    accepts_nested_attributes_for :propertytype
    accepts_nested_attributes_for :amenities
    accepts_nested_attributes_for :services

    mount_uploaders :pictures, PropertypictureUploader
end

用户模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable, :trackable

  validates :rfc, presence: true

end

所以,如何使用户看到一些属性?我将感谢您的帮助!

ruby-on-rails ruby ruby-on-rails-5 scaffold
1个回答
0
投票

下面是根据您上面的信息提供的示例,以查找所有者创建的属性

def index
  @properties = Owner.find_by_name('John').properties
  # this will list properties that owned by John
  @properties = Property.joins(:owner).all
  # this will list properties that has owner
  @properties = Property.joins(:owner).where('properties.price <= ?',1000000)
  # this will list properties that has owner and price is below 1 million
end
© www.soinside.com 2019 - 2024. All rights reserved.