Вопрос

still plugging at my first rails program ever (Ruby 2.0, Rails 4.0). Main model "Contact" should include a drop down in its form for "Color" table. I have added the Color model, created the html drop down and am trying to seed the Color model (drop down currently displays an empty menu). I am filling Color in seeds.rb from two arrays, and have verified that both arrays are full of string values (same vals as respective Color migration columns). When I attempt to put the array values into the Color table, it creates the right quantity of entries (my arrays are 140 elements in size each), but all entries are nil in both columns.

Below is my seeds.rb

Additional total noob question? How do I paste code into Stackoverflow on a linux machine instead of typing it?

colors = Array.new
colors = File.readlines("db/seeds/colornames.csv").map! {|name| name.chomp}

hexes = Array.new
hexes = File.readlines("db/seeds/colorhexes.csv").map! {|hex| hex.chomp}

Color.delete_all #because I keep having to reseed
x = 0
colors.each do |color|
  Color.create!(:name => color, :hex => hexes[x])
  x+=1
end

and the contact.rb

class Contact < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :email, :zip_code, :favorite_color, :color_id #I have brought in the right gem to use this older method
  belongs_to :color 

  #validation of fields other than :favorite_color in here. Nothing pertinent to this q

  #favorite color validation
  validates_presence_of :favorite_color

end

and color.rb

class Color < ActiveRecord::Base
  has_many :contacts
end
Это было полезно?

Решение 2

I am unsure what your issue is by looking at the code, I recommend some poor mans debugging - puts is your friend

Also can you post your model code?

A few rubyish changes you could make to your code

  • Array.new is rarely seen in ruby code, use [] instead, in this case no need to initialize the variable at all
  • no need to call map! in this case, map will do
  • Array (Enumerable) has a method each_with_index and you can avoid the x local
  • use ruby 1.9 hash syntax

example

colors = File.readlines("db/seeds/colornames.csv").map {|name| name.chomp}
hexes = File.readlines("db/seeds/colorhexes.csv").map {|hex| hex.chomp}

Color.delete_all # because I keep having to reseed

colors.each_with_index do |color, index|
  Color.create!(name: color, hex: hexes[index])
end

now with some debugging

colors = File.readlines("db/seeds/colornames.csv").map {|name| name.chomp}
hexes = File.readlines("db/seeds/colorhexes.csv").map {|hex| hex.chomp}

puts "Color Count: #{colors.length}"
puts "Hex Count: #{hexes.length}"

Color.delete_all # because I keep having to reseed

colors.each_with_index do |color, index|
  puts "#{color}: #{hexes[index}"
  Color.create!(name: color, hex: hexes[index])
end    

puts "Loaded: #{Color.count} colors"

Другие советы

Do You have this gem installed github.com/rails/protected_attributes ? If it's true, then You need to add as in Rails 3 this line to Your model:

attr_accessible :name, :hex
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top