0

Мой идентификатор пользователя не сохраняется, и я не понимаю, почему. Я добавил ассоциации к обоим моделям и добавил user_id к параметрам уведомления, но идентификатор пользователя не сохраняется автоматически.Мой идентификатор пользователя не сохраняется

Отношение campus_id работает нормально, и я не вижу разницы. В чем может быть проблема? Я использую postgress database.

рельсов журнал сервера

Rendered /usr/local/rvm/gems/[email protected]/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 
+0

Heads up - код отличный, однако нам нужны фактические данные об ошибках, включая вывод с номерами строк. _ ", но идентификатор пользователя не сохраняется автоматически. Отношение campus_id работает правильно ..." _ не говорит об этом. Если вы добавите это, вы получите более качественные ответы. Благодарю. –

+0

И что вы видите в журнале серверов rails? –

+0

Спасибо, я не получаю никаких сообщений об ошибках. Мой журнал сервера, который я собираюсь опубликовать. Может ли это что-то сделать с моими вложенными маршрутами? – Christoph

ответ

0

Поскольку user_id не является частью ваших параметров

Из журнала:

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

тогда линия от сильного Params фильтрует нежелательный контент

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

это только "notification"=>{"post"=>"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 приходит как часть уведомления, и это все!

+0

Спасибо! Я исправил это, добавив @ notification.user = current_user.id к моему действию action в контроллере :) – Christoph

Смежные вопросы