用户能够看到所有者属性 Rails 5.2
Users able to see Owners properties Rails 5.2
我一直在开发一个类似于 房地产 平台的应用程序。但是我在特定功能上遇到了儿子的困难。
我有两类用户:所有者和用户。所有者创建属性,用户显然需要将它们可视化并与之交互。两个用户都是使用 devise 开发的,所有者可以成功创建属性,但是 用户看不到它们 .
因此,当我以 User 身份登录时,我想查看所有者创建的一些属性,无论它们以何种顺序显示。我遇到这个问题是因为我需要创建一个过滤器,以便 用户 可以找到特定的属性,但如果用户看不到属性,则无法完成任何过滤器。
到目前为止,这是我的代码:
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
那么,我怎样才能让用户看到一些属性呢?我将不胜感激!
以下是根据您上面的信息得出的示例,用于查找由所有者创建的属性
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
我一直在开发一个类似于 房地产 平台的应用程序。但是我在特定功能上遇到了儿子的困难。
我有两类用户:所有者和用户。所有者创建属性,用户显然需要将它们可视化并与之交互。两个用户都是使用 devise 开发的,所有者可以成功创建属性,但是 用户看不到它们 .
因此,当我以 User 身份登录时,我想查看所有者创建的一些属性,无论它们以何种顺序显示。我遇到这个问题是因为我需要创建一个过滤器,以便 用户 可以找到特定的属性,但如果用户看不到属性,则无法完成任何过滤器。
到目前为止,这是我的代码:
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
那么,我怎样才能让用户看到一些属性呢?我将不胜感激!
以下是根据您上面的信息得出的示例,用于查找由所有者创建的属性
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