【发布时间】:2023-04-05 05:52:01
【问题描述】:
目前共有三种型号:users、teachers和posts。
我能够建立关联,以便教师可以看到他们的用户的帖子,这些关联(感谢迄今为止大家的帮助):
class Teacher < ActiveRecord::Base
has_many :users
has_many :posts, through: :users
def user_posts
Post.where(id: users.map(&:id))
end
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
另外,用户:belongs_to :teacher, has_many :posts
发帖:belongs_to :user
目标是让教师看到他们用户的帖子。但是,它只是部分起作用 - 错误是 当老师查看他的用户的帖子时,只显示少数帖子。
例如,用户 Corey 发布了 2 个帖子。但是他的老师菲尼先生只能看到其中的一条。我已尝试重置数据库并创建新条目,但问题仍然存在。有谁知道为什么会这样以及任何潜在的解决方案?
教师控制器
class TeachersController < ApplicationController
before_action :find_teacher, only: [:index]
def index
@posts = @findteacher.user_posts
@teachers = Teacher.all
end
def show
@teacher = Teacher.find_by_username(params[:id])
end
private
def find_teacher
@findteacher = current_teacher
end
end
用户控制器
class UsersController < ApplicationController
def self.find_for_authentication(conditions)
conditions = ["username = ? or email = ?", conditions[authentication_keys.first], conditions[authentication_keys.first]]
# raise StandardError, conditions.inspect
super
end
def index
@users = User.all
end
def show
@user = User.find_by_username(params[:id])
end
end
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to :back, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
帖子控制器
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
if params[:user_id]
@posts = Post.where(user_id: params[:user_id])
else
@posts = Post.all
end
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = current_user.posts.build
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to :back, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :username, :Duration, :Rating)
end
end
教师索引.html.erb
<p id="notice"><%= notice %></p>
<h1><%= current_teacher.username %> student posts</h1>
Studio Posts for <%= current_teacher.username %> (Your id: <%= current_teacher.id %>)
<p>
<table>
<thead>
<tr>
<th>UserID</th>
<th>User</th>
<th>Created At</th>
<th>Duration</th>
<th>Rating</th>
<th>TeacherID</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.user_id %></td>
<td><%= link_to post.user.username, post.user %></td>
<td><%= post.created_at %></td>
<td><%= post.Duration %></td>
<td><%= post.Rating %></td>
<td><%= post.user.teacher_id %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
架构(只是其中的一部分)
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.integer "teacher_id"
end
...
create_table "posts", force: :cascade do |t|
t.integer "user_id"
t.integer "Duration"
t.text "Rating"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "username"
...
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.integer "teacher_id"
end
【问题讨论】:
-
请显示无效的帖子数据,例如数据库行
-
根据你的情况,每个用户似乎只有一位老师?
标签: ruby-on-rails