gpt4 book ai didi

ruby-on-rails - has_one 关联和消失的用户 ID

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:38 25 4
gpt4 key购买 nike

我有一个错误,很可能是由我的模型中的 has_one 关联引起的。

问题

在声明这些关联后,我开始进行奇怪的交互,我突然收到 "undefined methodemail' for nil:NilClass"` 错误控制台中有以下代码段:

<div class="row">

<div class="column-md-6 column-md-offset-3">

<% @posts.each do |post| %>

email: <%= post.user.email %> <br> # <= source of the issue!

Level: <%= post.level %> <br>

Region: <%= post.region %> <br>

Info: <%= post.description %> <br>

<% if post.user == current_user %>

<%= link_to "Edit", edit_post_path(post) %>

问题来源

我很难找到错误并尝试重现它。我在 Rails 控制台中快速查看了我的数据库,发现 user_id 变成了 nil,即使从用户创建帖子是成功的。我终于按照以下步骤重现了错误:

1) 登录

2) 创建帖子

3) 转到“所有帖子”。帖子索引操作在此处正确呈现每个用户的所有帖子。

4) 在仍然以同一用户身份登录的情况下,创建另一个帖子。

5) 返回“所有帖子”,出现上述错误。

发布模型

class Post < ActiveRecord::Base
validates_presence_of :level, :region, :description
validates :level, length: { maximum: 50 }

belongs_to :user
end

用户模型

class User < ActiveRecord::Base  
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_one :post
end

帖子管理员

class PostsController < ApplicationController
before_action :set_post, only: [:edit, :update]
before_action :authenticate_user!, except: [:index, :show]

def index
@posts = Post.all
end

def show
@post = Post.find(params[:id])
end

def new
@post = current_user.build_post
end

def create
@post = current_user.build_post(post_params)
if @post.save
flash[:success] = "Post successfully created!"
redirect_to posts_path
else
render 'new'
end
end

def edit
end

def update
#@post = Post.find(post_params)
if @post.update(post_params)
flash[:success] = "Profile updated"
redirect_to posts_path
else
render 'edit'
end
end

def destroy
Post.find(params[:id]).destroy
flash[:success] = "Post deleted"
redirect_to posts_path
end

private

def post_params
params.require(:post).permit(:description, :level, :region)
end

def set_post
@post = Post.find(params[:id])
end

结束

schema.rb

ActiveRecord::Schema.define(version: 20151209193950) do

create_table "posts", force: :cascade do |t|
t.text "description"
t.string "level"
t.string "region"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end

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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

问题

我的目标是为每个用户设置一个帖子限制。我几乎可以肯定 has_one 协会是问题的一部分。我的问题是这是否是实现我的目标的正确方法,还是我应该使用 has_many 关联并以其他方式强制执行限制?

如果 has_one 是正确的方法,我该如何解决这个问题?

最佳答案

您必须在 Controller 的编辑操作中再次找到正确的用户并将其提供给 View ,否则实例为nil,从而导致您的错误。

Edit_0:
通常,在您发布的代码之前,添加:

<%= debug params %>

查看您在 View 中缺少/可用的变量。

关于ruby-on-rails - has_one 关联和消失的用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com