저는 RoR을 처음 사용 했으므로 앱을 개발하고 싶지만 belongs_to
연관 문제가 있습니다. 내 사용자의 인증을 위해 devise
을 사용하고 있는데 timesheet
이라는 객체가 있는데 여러 자습서를 읽고 많은 포럼을 읽었지만 불행히도 user_id
은 내 데이터베이스에 null
으로 남아 있기 때문에 문제가 어디서 발생하는지 알 수 없습니다.belongs_to 및 user (devise) 문제
당신이 그것을 고치는 방법을 말할 수 있다면, 나를 도울 수있는 모든 링크는 훌륭 할 것입니다.
Schema.rb :
ActiveRecord::Schema.define(version: 20150128160116) do
create_table "timesheets", force: true do |t|
t.date "date"
t.time "working_start_time"
t.time "working_end_time"
t.integer "breaks"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "timesheets", ["user_id"], name: "index_timesheets_on_user_id"
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.integer "current_sign_in_ip"
t.integer "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
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
timesheets_controller.rb
class TimesheetsController < ApplicationController
layout 'application'
def show
@timesheet=Timesheet.find(params[:id])
end
def index
@timesheet = Timesheet.all
end
def new
@timesheet = Timesheet.new
end
def create
@timesheet = Timesheet.create(timesheet_params)
redirect_to new_timesheet_path
end
def edit
@timesheet=Timesheet.find(params[:id])
end
def update
@timesheet = Timesheet.find(params[:id])
@timesheet.update_attributes(timesheet_params)
redirect_to student_table_path
end
user.rb 모델
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :timesheets
end
Timesheet.rb 모델 사전에
class Timesheet < ActiveRecord::Base
belongs_to :user
validates :user_id, :presence => true
end
감사합니다.
안녕하세요, RailsOuter, 답장을 보내 주셔서 감사합니다. 귀하의 코드를 시도했지만 지금은 이전에 작업 한 다른 필드를 포함하여 내 DB에 아무 것도 추가하지 않습니다. – reme
나는 시도해 본다. @timesheet = current_user.timesheets.create (timesheet_params) redirect_to new_timesheet_path end 이제는 작동한다. – reme