简体   繁体   中英

Cant mass assign protected attributes in Rails

Controller:

class HorsesController < ApplicationController
    require 'csv'

    def index
        @horses = Horse.all
    end

    def import
        Horse.import(params[:file])
        redirect_to root_path
    end
end

model:

class Horse < ActiveRecord::Base
  attr_accessible :name, :place

  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
        Horse.create! row.to_hash       
    end
  end
end

I have a CSV file that has two columns of data title name and place. When I try to import the file I get an error that says: Can't mass-assign protected attributes: place

I seems to accept :name fine but for some reason will not work with :place ???

Any help appreciated

Your csv file formatted like name, place instead of name,place . place is begin read as <space>place instead of place which makes rails throw that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM