简体   繁体   English

User_id在Rails关联上为nil

[英]User_id is nil on rails association

I have a user model and a shout model. 我有一个user模型和一个shout模型。 I am trying to have a user be associated with a shout. 我正在尝试让用户与喊话相关联。 I did not make this association upon creation of the shouts table so I had to run a new migration. 创建喊表时没有建立这种关联,因此我必须运行新的迁移。 Below is my table, the models of each, and the output when from my console I run a command to try and find the user_id of a shout. 下面是我的表,每个模型的模型以及在控制台上运行命令以尝试查找提示的user_id时的输出。 Can you see what I am doing wrong? 你能看到我做错了吗?

schema: 模式:

create_table "shouts", force: :cascade do |t|
    t.string  "title"
    t.text    "description"
    t.integer "user_id"
  end

  add_index "shouts", ["user_id"], name: "index_shouts_on_user_id"

  create_table "users", force: :cascade do |t|
    t.string   "email",           null: false
    t.string   "password_digest", null: false
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "username"
  end

User Model: 用户模型:

class User < ActiveRecord::Base
  validates :email, presence: true, uniqueness: true
  validates :password_digest, presence: true
  has_many :shouts
end

Shout Model: 喊模型:

class Shout < ActiveRecord::Base
    belongs_to :user
end

Shout Controller: 喊叫控制器:

class ShoutsController < ApplicationController 类ShoutsController <ApplicationController

def new
        @new_shout = Shout.new
    end

def create
    @new_shout = Shout.new(shouts_params)


    if @new_shout.user_id == nil
        render new_shout_path
    elsif @new_shout.save
        redirect_to dashboard_path
    else
        render new_shout_path
    end
end

private

def shouts_params
    params.require(:shout).permit(:title, :description, :user_id)
end

end 结束

Some test code: 一些测试代码:

> Shout.find(4)
> #<Shout id: 4, title: "four", description: "four", user_id: nil>

Creating an instance of user from the console, working: 从控制台创建用户实例,工作方式是:

> User.first.shouts.create(title: 'four', description: 'four')
>[["title", "four"], ["description", "four"], ["user_id", 1]

Migration file: 迁移文件:

class AddUserRefToShouts < ActiveRecord::Migration
  def change
    add_reference :shouts, :user, index: true, foreign_key: true
  end
end

Here are a couple admittedly hacky options (but they'll work) if you don't want to follow the approach suggested in the comments. 如果您不想遵循注释中建议的方法,那么这里有两个不错的选项(但它们可以起作用)。 You could pass the user_id as a hidden field so it'll be included in the params or you can expressly set it in the create action. 您可以将user_id作为隐藏字段传递,以便将其包含在参数中,或者可以在create动作中明确设置它。

If you want to pass as a hidden field, on your shout form add: 如果要作为隐藏字段传递,请在喊话表单上添加:

<%= f.hidden_field :user_id, value: current_user.id %>

Alternatively, if you want to handle in your create action: 或者,如果要在创建操作中处理:

def create
  @shout = Shout.new(shout_params)
  @shout.user_id = current_user.id
  @shout.save
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM