[英]Rail displaying only models whose parent model belong to the current user
I'm creating a Rails 5 app and have run into an issue where my Projects model index shows projects for all users on the platform. 我正在创建一个Rails 5应用程序,并且遇到了一个问题,我的项目模型索引显示了平台上所有用户的项目。
The way my app is structured is that I have a Project model which belongs to a Vehicle model which belongs to a user. 我的应用程序的结构方式是,我有一个属于车辆模型的项目模型,而该模型属于用户。 When I go to my Projects index, all the projects from other users also show. 当我转到“项目”索引时,也会显示其他用户的所有项目。 How would I get only projects that their parent Vehicle model have the User id for the current user? 我如何只获得其父车辆模型具有当前用户的用户ID的项目? Or would it be better to also add a user id to the Project Class. 或者最好也将用户ID添加到项目类中。
Here is my Project controller: 这是我的项目控制器:
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /projects
# GET /projects.json
def index
@projects = Project.where(completed: 'false')
end
def all_projects
@projects = Project.all
render :index
end
def finished_projects
@projects = Project.where(completed: 'true')
render :index
end
# GET /projects/1
# GET /projects/1.json
def show
@back_url = session[:my_previous_url]
end
# GET /projects/new
def new
@project = Project.new(vehicle_id: params[:vehicle_id])
@vehicles = Vehicle.where(user: current_user)
end
# GET /projects/1/edit
def edit
@vehicles = Vehicle.where(user: current_user)
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:title, :details, :vehicle_id, :completed)
end
end
I am using devise for authentication if that makes any difference. 如果这有任何区别,我正在使用devise进行身份验证。 Thanks! 谢谢!
Let's add some joins and a where condition to match the current user. 让我们添加一些联接和匹配当前用户的where条件。 Some alternatives. 一些替代方案。
no users join 没有用户加入
@projects = Project.joins(:vehicle).
where(vehicles: {user: current_user})
with user join 与用户加入
@projects = Project.joins(vehicle: :user).
where(users: {id: current_user.id})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.