여러분, 이것이 제 DB 구조입니다. AccountGrade.rb MODEL은 계정 및 등급 모델에 대한 조인 테이블입니다.
class AccountGrade < ActiveRecord::Base
belongs_to :account
belongs_to :grade
attr_accessor :grade
attr_accessor :section
end
내 account.rb 모델
class Account < ActiveRecord::Base
has_many :grades, :through => :account_grades
has_many :account_grades
belongs_to :user
validates :school_name,:line_1,:city,:state,:country,:pincode,:phone_number, :presence => true
validates :pincode,:phone_number,numericality: { only_integer: true }
end
내 grade.rb 모델
class Grade < ActiveRecord::Base
has_many :accounts, :through => :account_grades
has_many :account_grades
attr_accessor :account_grades
end
내 grades_controller.rb
class GradesController < ApplicationController
def index
@grades = Grade.all
render json: {
Grades:@grades
}.to_json
end
def add_class_sections
# unless params[:section]
@account_grades = AccountGrade.new class_sections_params
puts "Grades are #{@account_grades}"
@grades.each do |grade|
@account_grades = grade
puts grade
puts @account_grades
end
# end #unless ends here
end #function add_class_sections ends here
private
def class_sections_params
params.permit!
# params.require(:gardes).permit(:section)#, :email, :password, :salt, :encrypted_password)
end
end #class ends here
터미널 추적에서 아래 오류가 발생합니다.
Started POST "/add_classes?grade_id[0]=1§ion[0]=2&grade_id[1]=2§ion[1]=1&grade_id[2]=3§ion[2]=1" for 127.0.0.1 at 2015-11-17 12:43:47 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by GradesController#add_class_sections as */*
Parameters: {"grade_id"=>{"0"=>"1", "1"=>"2", "2"=>"3"}, "section"=>{"0"=>"2", "1"=>"1", "2"=>"1"}}
Completed 500 Internal Server Error in 10ms (ActiveRecord: 0.8ms)
ActiveRecord::UnknownAttributeError (unknown attribute 'controller' for AccountGrade.):
app/controllers/grades_controller.rb:11:in `add_class_sections'
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (11.0ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /home/anjan/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (24.8ms)
와우 코드에 많은 문제가 있습니다.
당신의 추론에 관계없이, 이것이 당신이 그것을 작동시키는 방법입니다 ...
#app/controllers/grades_controller.rb
class GradesController < ApplicationController
def index
@grades = Grade.all
respond_to do |format|
format.json { render json: @grades.to_json }
format.html
end
end
def create
@account_grades = AccountGrade.new class_sections_params
redirect_to @account.grades if @account_grades.save
end
private
def sections_params
params.require(:grades).permit(:section)
end
end
이 코드가 작동하면 controller
속성을 찾는 이유 (이상한 오류) 를 이해할 수있는 훨씬 더 강력한 위치에있게됩니다 .
또한 Rails의 몇 가지 규칙을 알고 있어야합니다.
puts
컨트롤러에 - 당신은 Rails.logger.info()
당신의 콘솔에 출력.add_class_section
경우가 아니면 호출 하지 마십시오. 대신 필요에 따라 new
& create
메서드를 사용해야합니다.-
또한 모든 attr_accessor
참조 , 특히 연관 이름과 충돌 하는 참조를 모두 제거하십시오 .
attr_accessor
기본적으로 모델에 대한 새로운 getter
/ setter
메소드를 선언 합니다. 이러한 메서드가 관계를 재정의하면 실제로 작동하지 못하게됩니다.
attr_accessor
반영구 속성 (DB에 저장하지 않는 IE 속성)을 생성하려는 경우 가 아니면 필요 하지 않습니다. attr_accessor
Rails에서 가장 일반적인 용도 는 가상 속성 을 만드는 것입니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다