【问题标题】:My user id isn't saving我的用户 ID 没有保存
【发布时间】:2014-10-18 16:56:00
【问题描述】:

我的用户 ID 没有保存,我不明白为什么。 我将关联添加到两个模型,并将 user_id 添加到通知参数,但用户 ID 不会自动保存。

campus_id 关系确实工作正常,我看不出有什么区别。可能是什么问题呢?我正在使用 postgress 数据库。

rails 服务器日志

  Rendered /usr/local/rvm/gems/ruby-2.1.1@rails4/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (59.4ms)




Started POST "/campus/1/notifications" for 84.193.153.106 at 2014-10-18 19:41:03 +0000
Started POST "/campus/1/notifications" for 84.193.153.106 at 2014-10-18 19:41:03 +0000
Processing by NotificationsController#create as HTML
Processing by NotificationsController#create as HTML
  Parameters: {"utf8"=>"_", "authenticity_token"=>"L9RH3hZyAKqzq9/kuJJDNaEHNVca2DbQSKSZLc8iTuw=", "notification"=>{"post"=>"thisisatest"}, "commit"=>"Maak notificatie", "campu_id"=>"1"}
  Parameters: {"utf8"=>"_", "authenticity_token"=>"L9RH3hZyAKqzq9/kuJJDNaEHNVca2DbQSKSZLc8iTuw=", "notification"=>{"post"=>"thisisatest"}, "commit"=>"Maak notificatie", "campu_id"=>"1"}
  Campus Load (0.6ms)  SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1  [["id", "1"]]
  Campus Load (0.6ms)  SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1  [["id", "1"]]
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.2ms)  BEGIN
  SQL (134.0ms)  INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["campus_id", 1], ["created_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00], ["post", "thisisatest"], ["updated_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00]]
  SQL (134.0ms)  INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["campus_id", 1], ["created_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00], ["post", "thisisatest"], ["updated_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00]]
   (42.1ms)  COMMIT
   (42.1ms)  COMMIT
Redirected to https://sl-backoffice-c9-christoph88.c9.io/notifications/9
Redirected to https://sl-backoffice-c9-christoph88.c9.io/notifications/9
Completed 302 Found in 188ms (ActiveRecord: 177.4ms)
Completed 302 Found in 188ms (ActiveRecord: 177.4ms)

通知模型

class Notification < ActiveRecord::Base
  belongs_to :campus
  belongs_to :user

  validates :post, presence: true
  validates :campus_id, presence: true
end

用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable

  has_many :notifications
  has_many :spotlights
end

通知控制器

class NotificationsController < ApplicationController
  before_action :set_notification, only: [:show, :edit, :update, :destroy]
  before_action :set_campus, only: [:index, :new, :create]
  before_action :authenticate_user!

  def index
    @notifications = @campus.notifications
  end

  def show
    @campus = @notification.campus
  end

  def new
    @notification = @campus.notifications.build
  end

  def edit
  end

  def create
    @notification = @campus.notifications.build(notification_params)

    respond_to do |format|
      if @notification.save
        format.html { redirect_to @notification }
        format.json { render action: 'show', status: :created, location: @notification }
        flash[:success] = 'Notification was successfully created.' 
      else
        format.html { render action: 'new' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @notification.update(notification_params)
        format.html { redirect_to @notification }
        format.json { head :no_content }
        flash[:success] = 'Notification was successfully updated.' 
      else
        format.html { render action: 'edit' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @notification.destroy
    respond_to do |format|
      format.html { redirect_to campu_notifications_url(@notification.campus_id) }
      format.json { head :no_content }
      flash[:error] = 'Notification was successfully deleted.' 
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_notification
      @notification = Notification.find(params[:id])
    end

    def set_campus
      @campus = Campus.find(params[:campu_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def notification_params
      params.require(:notification).permit(:post, :campus_id, :user_id)
    end
end

schema.rb

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

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

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "campus", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "collis", force: true do |t|
    t.string   "name"
    t.text     "teaser"
    t.text     "description"
    t.string   "coursetype"
    t.text     "target"
    t.string   "campus"
    t.datetime "startdate"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "guid"
  end

  create_table "coursespotlights", force: true do |t|
    t.boolean  "spotlight"
    t.integer  "colli_id"
    t.datetime "start"
    t.datetime "stop"
    t.string   "colli_guid"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "coursespotlights", ["colli_id"], name: "index_coursespotlights_on_colli_id", using: :btree

  create_table "notifications", force: true do |t|
    t.text     "post"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "campus_id"
    t.integer  "user_id"
  end

  add_index "notifications", ["campus_id"], name: "index_notifications_on_campus_id", using: :btree
  add_index "notifications", ["user_id"], name: "index_notifications_on_user_id", using: :btree

  create_table "spotlights", force: true do |t|
    t.boolean  "spotlight"
    t.datetime "start"
    t.datetime "stop"
    t.string   "name"
    t.text     "teaser"
    t.datetime "coursedate"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "campus_id"
    t.integer  "user_id"
  end

  add_index "spotlights", ["user_id"], name: "index_spotlights_on_user_id", using: :btree

  create_table "users", force: true 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.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

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

end

【问题讨论】:

  • 请注意 - 代码很棒,但是我们需要错误的实际细节,包括带有行号的输出。 " 但用户 id 不会自动保存。campus_id 关系确实可以正常工作..." 并没有告诉我们。如果你添加它,你会得到更好的答案。谢谢。
  • 另外,您在 rails 服务器日志中看到了什么?
  • 谢谢,我没有收到任何错误消息。我要发布的服务器日志。它可能与我的嵌套路由有关吗?

标签: ruby-on-rails activerecord ruby-on-rails-4 associations rails-postgresql


【解决方案1】:

因为 user_id 不是您的参数的一部分

来自日志:

Parameters: {"utf8"=>"_", "authenticity_token"=>"L9RH3hZyAKqzq9/kuJJDNaEHNVca2DbQSKSZLc8iTuw=", "notification"=>{"post"=>"thisisatest"}, "commit"=>"Maak notificatie", "campu_id"=>"1"}

然后来自强参数的行过滤掉不需要的内容

params.require(:notification).permit(:post, :campus_id, :user_id)

这意味着只允许"notification"=&gt;{"post"=&gt;"thisisatest"} 部分

然后从sql插入:

SQL (134.0ms)  INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["campus_id", 1], ["created_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00], ["post", "thisisatest"], ["updated_at", Sat, 18 Oct 2014 19:41:03 UTC +00:00]]

你在这里有 Campus_id,因为你正在 @campus 上进行更新:

@campus.notifications.build(notification_params)

所以,只要确保 user_id 作为通知的一部分出现就可以了!

【讨论】:

  • 谢谢!我通过将@notification.user = current_user.id 添加到控制器中的创建操作来修复它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多