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.
Related
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.
I am trying to add a new controller a Ruby on Rails 4 Spree -Ecommerce application. First of all, in routes.rb I added root :to => 'login#login' then in app/controllers/ I added a file called login_controller.rb and in the file I added the following code.
module Spree
class LoginController < Spree::StoreController
def login
render('spree/shared/_login')
end
end
end
When I start the server and go to localhost:3000/ I get this error
Unable to autoload constant LoginController, expected superclass mismatch for class LoginController
My goal here, is to require login to even view the home page of the store. I am attempting to build a site where users get a login screen when the go to it unless they are already logged in.
Please know that I am ruby noob and this is actually my first ruby on rails application so I am completely clueless here.
Also if there is a better way to go about doing what I want (requiring a login to basically view any page on the site) than my current apporach (having a LoginController which checks if there is a user logged in - if so redirect to home, if not redirect to Login) please let me know.
P.S. I got the layout for the controller from the home_controller.rb in the spree gem
I have not tested the code, but I suggest you use Spree's Devise integration
then you can add a before filter mandating authentication. Create a decorator to contain this logic addition. Create a file called base_controller_decorator.rb inside app/controllers/spree with the following code:
Spree::BaseController.class_eval do
before_filter :check_logged_in
def check_logged_in
unless spree_current_user
redirect_to spree_login_path
end
end
end
I guess if you are in the Spree module you don't need the namespace for the superclass class LoginController < Spree::StoreController like so class LoginController < StoreController.
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.
I'm trying to install Vanity A/B Testing in my Rails App, but I can't even get the example from the GitHub page working. I've generated the vanity models and run the migrations and made the experiment files, but as soon as I include a test like
<%= ab_test :price_options %>
the program throws an error:
invalid value for Integer(): "{:conditions=>{:experiment_id=>\"price_options\""
In my controllers/application_controller.rb I have just:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
use_vanity
end
I didn't include a user identifier because I haven't built one into this app yet, but the Vanity docs say that if no argument is supplied Vanity will just use cookies instead, so that shouldn't be an issue. If anyone has any clue why this cryptic error is being thrown, I would be very appreciative!
Edit: I should add that I actually started a new rails app from scratch just to try and debug this. Literally all I did was start an app and install vanity following the readme instructions and I got this same error. This has happened on two different machines as well, so I suspect it's something obvious that I'm missing, but I can't be sure (else I wouldn't be here!).
EDIT: I've since decided to use the Split gem instead, and am having no troubles with it.
This error is thrown because the current release version of Vanity uses the deprecated Rails find(:first) method, specifically:
VanityParticipant.first(:conditions=>{ :experiment_id=>experiment.to_s, :identity=>identity.to_s })
This no longer works with Rails 4.1 but is fixed in the master branch of Vanity, so you can get round this in your Rails 4.1 app by adding:
gem 'vanity', :git => 'git#github.com:assaf/vanity', :branch => 'master'
to your Gemfile.
With
class ApplicationController < ActionController::Base
protect_from_forgery
use_vanity
end
and
# experiments/price_options.rb
ab_test "Price options" do
description "Mirror, mirror on the wall, who's the best price of all?"
alternatives(19, 25, 29)
end
and
class StaticController < ApplicationController
def home
end
end
the following view loads and sets a vanity_id cookie:
<h1>Static#home</h1>
<p>Find me in app/views/static/home.html.erb</p>
<h2>Get started for only $<%= ab_test :price_options %> a month!</h2>
<%= link_to 'Make a new account', new_account_path %>
So there's a bit of code that I want to execute on every request that is made to my rails application and found through googling that I can do it using before_filter. Buy it doesn't seem to run when I go to the initial page, which is localhost:3000. Anyone know why it doesn't run?
I'm using Ruby on Rails 3, WebBrick server on Lubuntu Linux.
class ApplicationController < ActionController::Base
before_filter :run
private
def run
logger.debug "run? I'm too tired"
end
end
If your initial page is a 'static' page served from the public directory this code will not be run. To fix, make the page render as the result of some controller's action that inherits from ApplicationController.