1

這是我的數據庫結構。 AccountGrade.rb MODEL其連接表帳戶&檔次機型通過關聯在Rails 4中發佈連接表和has_many

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&section[0]=2&grade_id[1]=2&section[1]=1&grade_id[2]=3&section[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) 
+1

哎你的散列即參數結構,新的'AccountGrade'是錯誤的,因爲你有一個像'controller'和'action'這裏沒有必需的,這樣它可以讓你給錯誤 –

+0

嗨@VishalJAIN內容屬性,謝謝。 請你詳細說明一下,因爲我是一個鐵軌noob它不是很清楚我在哪裏worng? – Anjan

+0

目前還不清楚你在'add_class_sections'操作中要做什麼。但根據你的代碼片段,你正試圖創建'AccountGrade'在線沒有-11這是錯誤的,你提供了'class_sections_params'其中包含屬性名稱'controller',所以它給你上面提到的錯誤 –

回答

1

哇你的代碼有很多問題。

不管你的推理,這是你應該如何得到它的工作...


#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:

  1. 不要使用puts在控制器 - 你必須Rails.logger.info()輸出到您的控制檯。
  2. 保持行動寧靜 - 雖然這可以 - 而且往往是 - 破碎,一個Rails控制器的安寧性質應該是最重要的慣例遵循。除非您確實需要,否則不要致電add_class_section,而應根據需要使用new & create方法。

-

此外,刪除所有attr_accessor引用,特別是那些與任何關聯的名稱衝突。

attr_accessor基本上爲您的模型聲明新的getter/setter方法。如果這些方法覆蓋任何關係,他們實際上將阻止他們工作。

除非要創建半持久性屬性(IE不保存到數據庫),否則不需要attr_accessor。爲attr_accessor在Rails中最常見的用途是創建virtual attributes

enter image description here

0

這就是我想實現。歡迎任何關於重新考慮或改進以下代碼的建議。

class GradesController < ApplicationController 
    before_filter :authenticate, only: [:create] 

    def index 
     @grades = Grade.all 
     render json: @grades.to_json 
    end 

    def create 
     @section_name_counter = 0 
     @section_names=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S", 
        "T","U","V","W","X","Y","Z"] 
     @account_grades = AccountGrade.new #sections_params 
     @account_grades.grade_id = params[:grade_id] 
     @account_grades.account_id = params[:account_id] 
     @account_grades.save! 

     while @section_name_counter < params[:section].to_i do 
     @section = Section.new 
     @section.account_grade_id = @account_grades.id 
     @section.name = @section_names[@section_name_counter] 
     @section.save! 
     @section_name_counter += 1 
     end 
     render json: @account_grades if @section.save 
    end 

    private 

    def sections_params 
     params.require(:grade_id).permit(:section,:account_id) 
    end 
end 
相關問題