我創建2個模型支架:HAS_ONE,belongs_to的和create_before在軌
- 問題
- 記者
我要創建1名記者的每個問題,並使用before_create用於創建記者。
我通過整數類型在數據庫中的問題表中生成reporter_id。 下面的代碼顯示的問題,記者的行動和型號:
problem.rb
class Problem < ActiveRecord::Base
has_one :reporter
before_create :build_reporter
end
reporter.rb
class Reporter < ActiveRecord::Base
belongs_to :problem
end
problems_controller.rb
class ProblemsController < ApplicationController
before_action :set_problem, only: [:show, :edit, :update, :destroy]
def index
@problems = Problem.all
end
def show
end
def new
@problem = Problem.new
end
def edit
end
def create
@problem = Problem.new(problem_params)
respond_to do |format|
if @problem.save
format.html { redirect_to @problem, notice: 'Problem was successfully created.' }
format.json { render action: 'show', status: :created, location: @problem }
else
format.html { render action: 'new' }
format.json { render json: @problem.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @problem.update(problem_params)
format.html { redirect_to @problem, notice: 'Problem was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @problem.errors, status: :unprocessable_entity }
end
end
end
def destroy
@problem.destroy
respond_to do |format|
format.html { redirect_to problems_url }
format.json { head :no_content }
end
end
私人 高清set_problem @問題= Problem.find(PARAMS [:ID]) 端
def problem_params
params.require(:problem).permit(:reporter_id, :describe_id, :datetime, :trace_code, :status)
end
端
reporter_controller.rb
class ReportersController < ApplicationController
before_action :set_reporter, only: [:show, :edit, :update, :destroy]
def index
@reporters = Reporter.all
end
def show
end
def new
@reporter = Reporter.new
end
def edit
end
def update
respond_to do |format|
if @reporter.update(reporter_params)
format.html { redirect_to @reporter, notice: 'Reporter was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @reporter.errors, status: :unprocessable_entity }
end
end
end
def destroy
@reporter.destroy
respond_to do |format|
format.html { redirect_to reporters_url }
format.json { head :no_content }
end
end
private
def set_reporter
@reporter = Reporter.find(params[:id])
end
def reporter_params
params.require(:reporter).permit(:user_name, :gomrok_name, :phone_number)
end
end
當我選擇在本地主機的新問題按鈕:3000 /問題, textfields顯示,但是當完成textfield並選擇創建問題按鈕,我得到這個錯誤:
ActiveRecord::UnknownAttributeError in ProblemsController#create
unknown attribute: problem_id
Extracted source (around line #31):
respond_to do |format|
if @problem.save
format.html { redirect_to @problem, notice: 'Problem was successfully created.' }
format.json { render action: 'show', status: :created, location: @problem }
else
什麼是problem_id?!
我沒有使用這個。
我想在數據庫的問題表中設置記者ID;我如何使用before_create或after_create創建記者,並將問題表中的reporter_id保存起來?
那好吧,現在當我創建新的記者時,rails會與reporter_id產生新問題,但其他問題字段爲空;我在記者控制器的create action中使用「redirect_to:controller =>'problem',:action =>'new'」,但是當調用這個動作時,我得到這個錯誤:「ReportersController#create中的AbstractController :: DoubleRenderError」。如何在將數據保存到數據庫之前完成問題的字段? – mgh
很高興幫助:)。這是一個新問題,如果尚未解決,您可以發佈另一個問題。我或其他人會很樂意幫忙。我目前在您的ReportsController中沒有看到「創建」操作,因此無法評論太多。如果您嘗試在操作中呈現/重定向兩次,我建議您查看「DoubleRenderError」的操作。 –