This occurs when I try to run rails g bootstrap:themed Associations in my terminal:
C:/Users/ruby/.pik/rubies/Ruby-193-p327/lib/ruby/gems/1.9.1/gems/twitter-bootstrap-rails-2.1.9/lib/generators/bootstrap/themed/themed_generator.rb:87:in `block in retrieve_columns': undefined method `columns' for Association:Class (NoMethodError)
It just can't seem to work, I've tried many ways, searched everywhere, never succesful. I'm using Mongo.
I just got exact same error. I created two identical project - one with mongoid and one without. I only get the error on the mongoid project.
Found this workaround that seems to solve the problem:
Remove references to ActiveRecord (around line 87) in the file:
/home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bundler/gems/twitter-bootstrap-rails-b8b7eb22614a/lib/generators/bootstrap/themed/themed_generator.rb
I changed ...
def retrieve_columns
if defined?(ActiveRecord)
rescue_block ActiveRecord::StatementInvalid do
#model_name.constantize.columns
end
else
rescue_block do
#model_name.constantize.fields.map {|c| c[1] }
end
end
end
to this ...
def retrieve_columns
rescue_block do
#model_name.constantize.fields.map {|c| c[1] }
end
end
To get the views working I needed to make sure that my model class had a created_at field that wasn't nil (alternatively edit the generated views).
Hope this helps.
PS: Wow ... it seems you've got twitter-bootstrap-rails working on windows - I didn't know that was possible!
Related
I have an app where each Site belongs to a Trust and a Trust has many Sites.
I'm building the JSON result with Jbuilder like this:
json.array! (#sites) do |site|
json.id site.id
json.name site.name
json.city site.city
json.state site.state
json.country site.country
json.start_date site.start_date
json.end_date site.end_date
json.trust site.trust, :id, :name # throws error here
end
The final line of the block returns an error of NoMethod, "a undefined method `id' for nil:NilClass."
However, the Site model belongs_to :trust and the Trust model has_many :sites.
When I run the Rails console, I can access site.trust.id and site.trust.name as I would expect.
Any ideas what's wrong?
I got it to work. I basically dumped my database and then ran the migrations again. It now works fine.
Any ideas why this would be? It worked fine previously, but then it threw the error. I just want to make sure I don't make the same mistake again.
I am trying to use Roo to import data from an Excel spreadsheet into a table (data_points) in a Rails app.
I am getting the error:
undefined method `fetch_value' for nil:NilClass
and that error references my data_point.rb file at line (see below for full code excerpt):
data_point.save!
The "Application Trace" says:
app/models/data_point.rb:29:in `block in import'
app/models/data_point.rb:19:in `import'
app/controllers/data_points_controller.rb:65:in `import'
I am puzzled by this because a "find all" in my entire app shows no instance of fetch_value
Here is the other code in my app:
In my model, data_point.rb:
class DataPoint < ActiveRecord::Base
attr_accessor :annual_income, :income_percentile, :years_education
def initialize(annual_income, income_percentile, years_education)
#annual_income = annual_income
#income_percentile = income_percentile
#years_education = years_education
end
def self.import(file)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..11).each do |i|
annual_income = spreadsheet.cell(i, 'A')
income_percentile = spreadsheet.cell(i, 'B')
years_education = spreadsheet.cell(i, 'C')
data_point = DataPoint.new(annual_income, income_percentile, years_education)
data_point.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".xlsx" then Roo::Excelx.new(file.path)
else raise "Unknown file type: #{file.original_filename}"
end
end
end
In my controller, data_points_controller.rb I have added, in addition to the standard rails framework:
def import
DataPoint.import(params[:file])
redirect_to root_url, notice: "Data points imported."
end
In the Excel file I'm using, the header row has column names that are exactly the same as the 3 attributes noted above for DataPoints: annual_income, income_percentile, years_education
P.s. I have already watched RailsCast 396: Importing CSV and Excel, and read the comments many times. I think I am struggling with translating the example code to Rails 4 and / or my assignment of individual attributes (vs. the approach used in the RailsCast).
Thanks in advance for any help!
I ran into a similar error also when trying to use Active Record with a legacy database. The problem for me was related to the fact that one of the columns of my database was named 'class,' which caused all sorts of things to fail. I renamed the column in the legacy database and everything worked fine.
Moral of the story- check the column names for any reserved words.
It seems you had some leftovers from your non rails practice, as we found in the comments. Notably, the overwritten initialize method, and the attr_accessor for each of the attributes. Removing them (and fixing the DataPoint.new() for the correct format) was all that was needed.
This is the problem encountered yesterday, the reason is I defined a Database field with name class, which is a reserved word for the Ruby language. It caused the conflict.
Remove your def initialize() method
If you are not restricted to delete the initialize method for some reason, instead of removing it, you can try to add a super call like so:
def initialize(annual_income, income_percentile, years_education)
super()
#annual_income = annual_income
#income_percentile = income_percentile
#years_education = years_education
end
Sources:
More info about super can be found here.
in my listing model i setup a method
def set_listing_number
listing_number="TM#{created_at.year}#{id}"
end
i have a couple of records that was created before i ran the migration to create the listing_number column. i wanted to update all the records at once but keep receiving this error.
here's the code that i ran that produce the error in console verifying if the method . it works i cannot save the assignment.
listing_number=listing.set_listing_number
=> "TM2014574"
2.0.0-p247 :003 > listing_number.save
NoMethodError: undefined method `save' for "TM2014574":String
i tried a couple with no success , that i also found here like this one
Listing.all.each{|n| n.update_attributes(:listing_number =>"TM#{n.created_at.year})}
the question how do i update the previous record at once. probably it's pretty basic but can't figure it out. thanks
# This method in Listing Model
def set_listing_number
listing_number="TM#{created_at.year}#{id}"
end
# In controller
Listing.all.each do |listing|
listing.set_listing_number
listing.save
end
or you can refactor this as
Listing.all.each do |listing|
listing.update listing_number: "TM#{listing.created_at.year}"
end
You're calling save on a string. You need listing.save in your code.
I just upgraded to rails 4 and ActiveAdmin 1.0. I'm having problem with page that won't work anymore. Here is the error:
undefined method `exclude_contest_eq' for Ransack::Search<class: Project, base: Grouping <combinator: and>>:Ransack::Search
Here is where the code is failing
controller do
def render(*args)
#projects.uniq! if #projects and action_name == 'index'
super(*args)
end
end
What has changed that is throwing this error?
It has something to do with filtering.
Here is the thread with long discussion of the issue.
The essence of it, that quickest fix would be, probably, to add remove_filter :exclude_contest to the model definition.
May this will solve your problem as well.
I'm using the rails-footnotes gem in my Rails 3.2 applications, but I can't seem to get the footnotes to register the existence of any partials: it always shows a zero partial count.
To be able to know how many and what partials are being displayed on a view easily like this is, I think, immensely useful, so I would really like to get this working (the rest works great), but I'm not sure what the problem is and am hoping someone else has had the same problem and was able to resolve it. Is there a setting I've potentially missed?
I don't think it's relevant, but I'm using OSX 10.6.8 and had some issues getting the gem to work with Sublime Text 2 properly, but they did get resolved (details in this StackOverflow answer).
Update:
It seems that the issue only exists for haml templates, as I am getting expected output for erb templates. It would seem that only erb templates are counted/recognized...?
Update 2:
#DonHopkins' answer below got all my Haml templates to register with Rails Footnotes. I put it in my config file as follows:
config/initializers/rails_footnotes.rb
if defined?(Footnotes) && Rails.env.development?
Footnotes.run! # first of all
Footnotes::Notes::LogNote::LoggingExtensions.module_eval do
def add(*args, &block)
super
logged_message = args[2] + "\n"
Footnotes::Notes::LogNote.log(logged_message)
logged_message
end
end
# ... other init code
Footnotes::Filter.prefix = 'subl://open?url=file://%s&line=%d&column=%d'
end
I had a similar problem, although I am using erb templates, not haml. I fixed it with a monkey patch to rails-footnotes.
Looking at the rails-footnotes code (version 3.7.9), it looked to me like the problem is in this method:
module Footnotes
module Notes
class LogNote < AbstractNote
...
module LoggingExtensions
def add(*args, &block)
logged_message = super
Footnotes::Notes::LogNote.log(logged_message)
logged_message
end
end
...
end
end
end
The add method is assuming that super returns the message that is being logged, but in my testing super was returning a boolean value. To solve the problem, I created a file called footnotes_patch.rb with the following:
Footnotes::Notes::LogNote::LoggingExtensions.module_eval do
def add(*args, &block)
super
logged_message = args[2] + "\n"
Footnotes::Notes::LogNote.log(logged_message)
logged_message
end
end
If you want to try the solution, put that file in config/initializers, then restart your application.