活动记录::未知属性错误在报表控制器#导入未知属性'relation'学生



我正在通过Excel将一些数据导入我的Rails应用程序。导入工作正常,但是当我尝试导入守护者文件时,出现错误:

学生的未知属性"关系"。

此字段不属于学生,但在导入过程中,当我选择学生导入文件时,它会以正确的方式插入。但是当我导入守护者文件时,它向我显示错误。

我试图调试代码params[:role] == :guardian ? Guardian : Student

我明白了

`[1] pry(#<ReportsController>)> params[:role] == :guardian ? Guardian : Student
=> Student(id: integer, admission_no: string, class_roll_no: string, admission_date: date, first_name: string, middle_name: string, last_name: string, batch_id: integer, date_of_birth: date, gender: string, blood_group: string, birth_place: string, nationality_id: integer, language: string, religion: string, category_id: integer, address_line1: string, address_line2: string, city: string, state: string, pin_code: string, country_id: integer, phone1: string, phone2: string, email: string, immediate_contact: integer, is_sms_enabled: boolean, status_description: string, is_active: boolean, is_deleted: boolean, created_at: datetime, updated_at: datetime, image_file_name: string, image_content_type: string, image_file_size: integer, image_updated_at: datetime)`

它只转到 ELSE 部分,只接受学生文件。

控制器

def importer      
  params[:role] == :guardian ? Guardian : Student
end
def import
  importer.import(params[:file])
  redirect_to import_reports_path, notice: "Students imported."
end

学生.rb

COLUMNS_TO_STRING = ["batch_id", "class_roll_no","phone1","phone2"]  # and so on
def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
  row = Hash[[header, spreadsheet.row(i)].transpose]
  row = clean_for row, COLUMNS_TO_STRING
  record = Student.find_by(:id => row["id"],:batch_id =>    row["batch_id"],:class_roll_no => row["class_roll_no"],:phone1 =>  row["phone1"],:phone2 => row["phone2"]) || new
  record.attributes = row.to_hash.slice(*row.to_hash.keys)
  record.save!  
 end
end

卫报.rb

COLUMNS_TO_STRING = ["student_id"] # and so on
def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
  row = Hash[[header, spreadsheet.row(i)].transpose]
  row = clean_for row, COLUMNS_TO_STRING
  guardian = Guardian.find_by(:id => row["id"],:student_id => row["student_id"]) || new
  guardian.attributes = row.to_hash.slice(*row.to_hash.keys)
  guardian.save!
 end
end

视图

<h1>IMPORT</h1>
<div class = "well">
    <p>
        <%= form_tag import_reports_path do %>
        <%= file_field_tag :file%>
        <br>
        <%= submit_tag "Import",class: "btn btn-primary" %>
        <% end %>
    </p> 
</div>

你确定不是字符串吗?尝试

def importer      
  params[:role] == 'guardian' ? Guardian : Student
end

最新更新