I've followed Michael Hartl's Ruby on Rails Tutorial making an app like Twitter and I want to change it so that all new users to automatically 'follow' the Admin/s.
I've tried to do it following answers to similar questions here and here, but they throw errors on account creation. Here's the relevant section of my user controller.
Edit: find_all_by_admin is deprecated in Rails 4.2.0 as explained in the selected answer by Vinay.
controllers/users_controller.rb
class UsersController < ApplicationController
...
def follow_admins
admins = User.find_all_by_admin(true) # EDIT - Deprecated: May have worked prior to rails 4.2
admins.each do |admin|
self.follow!(admin)
end
end
The error message is
NoMethodError in UsersController#create
undefined method `find_all_by_admin' for #
It seems to me that there is no column with admin name in your User model as we might can see Michael Hartl's sample_app_3rd_edition
So in order to make follow_admins method work you need to add admin column in users table type boolean and default: false.
def follow_admins
admins = User.find_all_by_admin(true) # would be worked in rails 4.0 not rails 4.2.2
admins = User.where(admin: true) # Should be work in rails 4.2.2
# Most of the Dynamic finder has been removed form rails 4.2.2
admins.each do |admin|
self.follow!(admin)
end
end
note As I mention in my answer default to false ,It is not mandatory but as you are following Michael Hartl's Ruby on Rails Tutorial It good to go accordingly .
hope this answer would help you !!!
It's likely you never defined the find_all_by_admin method in the User model.
Open the User model file and check if the method is there. If not, define it as a class method.
Related
I am trying to get text preceding '#' become a hashtag in my Rails 4.0.0 app. I am trying to use the ruby gem simple_hashtag
for that purpose. The error I'm getting in my post_controller.rb is: undefined method 'hashtags' for #<ActiveRecord::Relation::ActiveRecord_Relation_Post...>
Here is the action in which this error occurs:
def index
#posts=Post.sorted
#posts.hashtags.each do |hashtag|
link_to hashtag.name, path_to_hashtag(hashtag.name)
end
end
Here is the model for Post i.e. post.rb
class Post < ActiveRecord::Base
include SimpleHashtag::Hashtaggable
hashtaggable_attribute :text
end
Anybody can help me run this gem? Thanks in advance.
As the error is hashtags method is not available for ActiveRecord::Relation::ActiveRecord_Relation_Post...>
As hashtags is an object of SimpleHashtag::Hashtag class. Further this gem would create simple_hashtag_hashtags table.
simple_hashtag_hashtags should not be blank. If this return as a blank means have no record and would throw error as undefined .In case if table have no record it seems you would have made any mistake.
def index
#hashtags = SimpleHashtag::Hashtag.all
#hashtags.each do |hashtag|
link_to hashtag.name, path_to_hashtag(hashtag.name)
end
end
The gem is not maintained actively. The last commit is already 2 years old. It may not be working with recent versions of Rails.
I've had the following setup to send the user a welcome email after a successful registration.
class RegistrationsController < Devise::RegistrationsController
def create
super
unless #user.invalid?
UserMailer.delay.welcome(#user)
end
end
end
I'm on Rails 4.1.4 and I recently updated all gems, which updated Devise to 3.2.4. After the update, the above started throwing the following error:
wrong number of arguments (1 for 2)
app/controllers/registrations_controller.rb:4:in `create'
Seems like calling super is what is breaking things.
Please advice.
This wasn't Devise fault, but rather the following Turbolinks change broke redirect_to https://github.com/rails/turbolinks/commit/153f1b0f04c718442cfd73365a2778dfe1a1c5c7
Rolling back to Turbolinks 2.2.2 or 3f2b6e752acde1d9a59a75c48401dfb152afe154 solved the problem.
On Rails 4 with the latest version of Active Admin (using Ransack). I have an Award model that I made available globally in my application_controller.rb:
before_action :set_awards
private
def set_awards
#awards = Award.all
end
This is so I could have a global navigation dropdown listing all awards (on the public side). I think this is conflicting with Active Admin. When I went to the Awards index page, I got the following error message:
Collection is not a paginated scope. Set collection.page(params[:page]).per(10) before calling :paginated_collection.
I am not sure what this means exactly but since this is the only model this happens to, I'm guessing it has something to do with my code in the app controller. I think my problem is similar to this question:
Rails 3.2 ActiveAdmin 'Collection is not a paginated scope.' error
...I would prefer not to change the model's label and the metasearch code provided doesn't work because (I think) Active Admin now uses Ransack. Thanks for any help.
I have had the same problem and it was a variable name i was declaring at the application controller that was conflicting with the ActiveAdmin.
I have a model called Post and in my applicationController i had a method like this:
def load_posts
#posts = Post.all
end
It returned me the error you mention, so i fixed it changing the variable name to:
def load_posts
#post_list = Post.all
end
Hope it helps.
May be you overwrited a local variable #awards in ApplicationController, but Active Admins is inheriting from the application controller.
Just rename your variable and it will run.
I'm trying to get my admin avatar from Gravatar using my email address. I copied some code from Ryan Bate's Railscast http://railscasts.com/episodes/244-gravatar
The application helper:
module ApplicationHelper
def avatar_url(current_admin)
if current_admin.avatar_url.present?
current_admin.avatar_url
else
default_url = "#{root_url}images/guest.png"
gravatar_id = Digest::MD5.hexdigest(current_admin.email.downcase)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
end
end
end
My Application controller has helper :all
My view has <%= image_tag, avatar_url(current_admin), :class => "img-circle" %> The error I get is NoMethodError and more specifically, undefined method 'avatar_url' for #<Admin:0x007fc4ae804ee8>
The current_admin is set by devise.
I looked through this RailsCast and it is very poor actually. At one point Ryan says just to add avatar_url to User without giving any hint where data for this field should come from.
So unfortunately you need to add this field by yourself by creating a rails migration if using ActiveRecord.
rails g migration AddAvatarUrlToUser avatar_url
But this is ONLY required if you want to allow users having their own non-gravatar images. If you want to stick with gravatar ones you can skip everything from section "Using Images From Other Sites With OmniAuth"
The error is telling your that the attribute avatar_url does not exists in your Admin model.
You should add it creating a rails migration if using ActiveRecord.
I had a two month break using Rails and now its biting me when I return!
I am trying to get CanCan to work on Rails 3.1 and have viewed the railscast and then followed their instructions on the cancan git page. However when I try run the homepage it gives me the following error:
Routing Error
undefined local variable or method `authorize_resource' for StaticController:Class
How do I make this stop? Its as if cancan is not loaded, but I have installed it using bundler and it doesnt mention that I have to do anything else to include it.
Anyone have any ideas?
Sorry my bad! Too much coffee resulted in me not reading all the instructions for how to install it.
I needed to add the following to my ApplicationController:
def current_ability
#current_ability ||= Ability.new(current_user)
end
and then I needed to use
class StaticController < ApplicationController
authorize_resource :class => false
Because in this particular example it was just a static simple homepage that sits infront of a more complicated web app.
Thanks for the help.