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
Related
I'm trying to create a login system with devise, cancancan and rolify. The devise part is working. I recently added cancancan and rolify and I'm trying to test if they are working.
My cancancan ability file:
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :admin
can [:index], Page
else
can [:index,:lecture]
end
end
end
I have a root page root to: "pages#index" which is working. If I add load_and_authorize_resource at the top of the pages controller like
class PagesController < ApplicationController
load_and_authorize_resource
def index
end
def lecture
end
end
I get back when trying to access it:
NameError in PagesController#index generate by these lines:
names.inject(Object) do |constant, name|
if constant == Object
* constant.const_get(name)
else
candidate = constant.const_get(name)
next candidate if constant.const_defined?(name, false)
The * is the line that gives the error. I also saw in the rolify documentation that I have to add resourcify to the files that are going to make use of it. But if I try to add it I get an error undefined method resourcify. How can I solve this ?
Check the following:
GemFile:
gem 'load_and_authorize_resource'
# bundle install after
ApplicationController:
class ApplicationController < ActionController::Base
include LoadAndAuthorizeResource
end
PagesController
class PagesController < ApplicationController
load_and_authorize_resource
....
More Info here Load And Authorize Resource
Once in a while when trying to fetch an order record for a particular user, a ActiveRecord::RecordNotFound is raised.
Some things to note here.
The error is raised when visiting /orders/:id, but not for all users. We track completed orders (meaning that you end up on a orders page) and around 50% gets a 404. Note that we're talking about 50% of the users, not the requests. If it displays 404 once for an order for a particular user, it will always display a 404.
The record exists as it can be accessed via the console using the same data that's being logged in the controller.
The problem disappears when re-deploying the application.
What could the problem be?
I'm running rails 4.2.0.
class OrdersController < ApplicationController
#
# GET /orders/:id
#
def show
Rails.logger.info "current_user=#{current_user.id}, params[:id]=#{params[:id]}"
#order = current_user.orders.find(params[:id])
end
end
class ApplicationController < ActionController::Base
def current_user
#_current_user ||= User.find_by_id(cookies.signed[:uid])
end
end
class User < ActiveRecord::Base
has_many :subscriptions
has_many :orders, through: :subscriptions
end
class Order < ActiveRecord::Base
has_one :user, through: :subscription
end
class Subscription < ActiveRecord::Base
belongs_to :user
end
Here's the log output
[User#id=2454266] Parameters: {"id"=>"1553"}
[User#id=2454266] current_user=2454266, params[:id]=1553 <==== Rails.logger.info
[User#id=2454266] Completed 404 Not Found in 240ms
[User#id=2454266]
ActiveRecord::RecordNotFound (Couldn't find Order with 'id'=1553):
app/controllers/orders_controller.rb:6:in `show'
Running User.find(2454266).orders.find(1553) in the console works.
Also note that it's possible to skip the relation and go directly to the order model, like this
class OrdersController < ApplicationController
#
# GET /orders/:id
#
def show
#order = Order.find(params[:id])
end
end
The way we arrived at the conclusion can be found by going through the comments list.
Our summary of findings are:
Either orders are cached, or current_user is outdated (along with cached associations)
Going straight to order works (i.e. Order.find / User.find(current_user.id).orders...
Solution that we arrived at is:
current_user.reload!
Before performing
current_user.orders.find(params[:id])
class ApplicationController < ActionController::Base
def current_user
#_current_user ||= User.find_by_id(cookies.signed[:uid])
end
def user_signed_in?
#_current_user.present?
end
end
class OrdersController < ApplicationController
def show
if user_signed_in?
#order = current_user.orders.find(params[:id])
else
return redirect_to <404 page or any>, alert: 'Order not found'
end
end
end
I think you have to check the presence of the user first, then you can trace the bug
I usually use this method when I got some issue
def show
if user_signed_in?
begin
#order = current_user.orders.find(params[:id])
rescue => e
binding.pry
end
else
return redirect_to <404 page or any>, alert: 'Order not found'
end
end
but you need 'rails-pry' gem, the advantage is rails will fire up rails console when there is exception
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?
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
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