my controller:
class Spree::CollectionPagesController < Spree::StoreController
def index
#collection_pages = CollectionPage.all
end
end
I get : uninitialized constant Spree::CollectionPagesController::CollectionPage
with:
class Spree::CollectionPagesController < Spree::StoreController
end
I get empty undefined method `each' for nil:NilClass
Because I'm doing 1 - #collection_pages.each do |collection_page| in views.
This is my model
module Spree
class CollectionPage < ActiveRecord::Base
default_scope order('position')
end
end
I inspected in the console, with Spree::CollectionPage.all and its not empty.
also everything is working fine in admin, not sure what I'm doing wrong.
any help would be greatly appreciated.
I believe you need to scope it.
Spree::CollectionPage.all
Related
Im creating an application where an user can see what workout of the day a trainer will tell him to do. This workout is going to be in the homepage of the app.
The trainer is going to upload the wods weekly, i want to show just one wod per day.
I thought that if i gave the WOD model an integer attribute called wday, so i can do the logic if Wod.wday == Date.today.cwday i would achieve this. This is not working at all, im getting an error at my static_pages controller.
Heres the code:
Wod model:
class Wod < ApplicationRecord
belongs_to :user
def this_day
Date.today.cwday == self.wday
end
end
User model:
class User < ApplicationRecord
has_many :wods
def feed
Wod.this_day
end
Static pages controller:
class StaticPagesController < ApplicationController
def home
if logged_in?
#wod = current_user.wods.build
#feed_items = current_user.feed
end
end
def contact
end
def about
end
end
Im getting the following error:
NoMethodError in StaticPagesController#home undefined method
`this_day' for #<Class:0x007f5870c1e7b8> Did you mean? third
I would like to know how to get this done, any help would be appreciated!
BTW: It works perfectly if i do in the user model Wod.all, but i dont want this
NoMethodError in StaticPagesController#home undefined method
`this_day' for #<Class:0x007f5870c1e7b8>
The problem lies here Wod.this_day. If you want to call this_day on WOD then you need to add it as a class method.
Change
def this_day
Date.today.cwday == self.wday
end
to
def self.this_day
Date.today.cwday == wday
end
Alternate solution is to use scopes.
class Wod < ApplicationRecord
belongs_to :user
scope :this_day, -> { where(wday: Date.today.cwday) }
end
I fixed it by changing the method this_day to:
def self.this_day
where("wday = ?", Date.today.cwday)
end
Today, I found a very strange issue while writing a mountable engine using Rails 3.
I have the following ApplicationController in my engine:
module Marketplace
class ApplicationController < ::ApplicationController
before_filter :merge_abilities
layout 'marketplace/application'
def marketplace_current_user
Marketplace::User.find(current_user.id)
end
private
def merge_abilities
current_ability.merge(Ability.new(current_user))
end
end
end
And my User model definition is
module Marketplace
class User < ::User
devise omniauth_providers: [:facebook, :paypal]
has_one :payout_identity, class_name: "Marketplace::PayoutIdentity"
has_many :course_purchases, class_name: "Marketplace::CoursePurchase"
def has_verified_payout_identity?
self.payout_identity and self.payout_identity.receiver_id
end
end
end
After starting up the rails server, the first request to load a page will have the controller run the marketplace_current_user method correctly and load the engine's User class. However any request after the first one will given a strange NameError - "uninitialized constant Marketplace::Marketplace::User".
I tried removing the namespace in marketplace_current_user definition but it will load the main app's User class instead.
At last when I change my ApplicationController to look like this:
class Marketplace::ApplicationController < ::ApplicationController
before_filter :merge_abilities
layout 'marketplace/application'
def marketplace_current_user
Marketplace::User.find(current_user.id)
end
private
def merge_abilities
current_ability.merge(Ability.new(current_user))
end
end
Everything would work fine.
Could someone enlighten me where I got it wrong at the beginning? Was it wrong to do inheritance that way?
I have the following model in my Rails 3.2.13 build. I am trying to use it to insert data into my database.
class Financials < ActiveRecord::Base
#attr_accessible :description, :stock
attr_accessible :symbol, :cur_price
sym = Financials.new(:symbol => test, :cur_price => 10)
sym.save
end
but when I try to run the code I get the following error:
financials.rb:1:in `': uninitialized constant ActiveRecord (NameError)
I checked through SO and found others that had similar errors and they suggested that I add entries in the environment.rb ruby on rails pluralization help?
I added the following to the environment.rb file:
Inflector.inflections do |inflect|
inflect.irregular 'financialss', 'financials'
end
but it did resolve my issue. Thanks in advance
You don't create new objects inside the definition of the model. You should be doing this in the create action of the controller.
Given your model:
class Financial < ActiveRecord::Base
attr_accessible :symbol, :cur_price
# validations, methods, scope, etc.
end
You create the new Financial object in your controller and redirect to the appropriate path:
class FinancialsController < ApplicationController
def create
#financial = Financial.new(params[:financial])
if #financial.save
redirect_to #financial
else
render :new
end
end
def new
#financial = Financial.new
end
def show
#financial = Financial.find(params[:id])
end
end
I have a rails model located at app/models/scheduling/availability.rb which looks like:
class Scheduling::Availability < ActiveRecord::Base
end
I have a Rails controller located at *app/controllers/admin/scheduling/availabilities_controller.rb* which looks like:
class Admin::Scheduling::AvailabilitiesController < ApplicationController
def index
#availabilities = Scheduling::Availability.all
end
end
My routes look like:
namespace :admin do
namespace :scheduling do
resources :availabilities
end
end
When trying to load the url:
/admin/scheduling/availabilities
I get the error:
uninitialized constant
Admin::Scheduling::AvailabilitiesController::Scheduling
I have a feeling this is because Rails is confusing the Scheduling module/namespaces.
What am I doing wrong?
Found my answer in another answer.
Need to preface my module with ::
class Admin::Scheduling::AvailabilitiesController < ApplicationController
def index
#availabilities = ::Scheduling::Availability.all
end
end
So I'm trying out the Public Activity gem for a notification timeline and I don't understand why I'm getting the NameError , uninitialized constant message.
Here's the error I'm getting:
NameError in ActivitiesController#index
uninitialized constant ActivitiesController::PublicActivity
app/controllers/activities_controller.rb:3:in `index'
uninitialized constant ActivitiesController::PublicActivity
Here's my controller code where the error is said to be:
class ActivitiesController < ApplicationController
def index
#activities = PublicActivity::Activity.order("created_at desc")
end
end
I'm trying this out based on a rails cast: http://railscasts.com/episodes/406-public-activity
And as far as I know I've done the same thing as the one on it.
There isn't any activity model, but here's the post and comments model where I used the PublicActivity module:
POST MODEL:
class Post < ActiveRecord::Base
include PublicActivity::Model
tracked
COMMENT MODEL
class Comment < ActiveRecord::Base
include PublicActivity::Model
tracked
belongs_to :post
belongs_to :user
end
according to gem sources
def load_orm
require "public_activity/orm/#{##orm.to_s}"
m = "PublicActivity::ORM::#{##orm.to_s.classify}".constantize
::PublicActivity.const_set(:Activity, m.const_get(:Activity))
::PublicActivity.const_set(:Adapter, m.const_get(:Adapter))
::PublicActivity.const_set(:Activist, m.const_get(:Activist))
::PublicActivity.const_set(:Trackable, m.const_get(:Trackable))
end
You should use next code
#activities = ::PublicActivity::Activity.order("created_at desc")
Also ensure that you installed gem with bundle, check bundle show public_activity