I've generated a mailer for an Order confirmation. I'm getting a NameError at /purchases.
The error reads:
uninitialized constant PurchasesController::UserMailer
/controllers/purchases_controller.rb
class PurchasesController < InheritedResources::Base
before_filter :authenticate_admin_user!, :only => [:index, :edit, :update, :destroy]
def create
#purchase = Purchase.new(params[:purchase])
if #purchase.save
UserMailer.purchase_confirmation(#purchase).deliver
redirect_to "/thankyou"
else
render :action => "new"
end
I've been digging around and have found similar issues, but nothing as of yet to solve my problem. Any help?
It may be cause by two problems and the following is just an Idea...
1) You need to restart the rails server after you added the Mailer
(or)
Try to run in your production server and check it.
2) check the spell of UserMailer should be user_mailer.rb
(or)
Make sure that you are using UserMailer.
UserMailer.rb will break whereas user_mailer.rb is what is expected.
Let us know once its not solved the above two options
Related
I got a really strange issue here. Here is the line causing all the trouble in my ability.rb
can [:edit, :update, :destroy], User, id: user.id
When I launch the rails console, I got the expected behaviour:
u = User.last
a = Ability.new(u)
a.can?(:edit, u)
=> true
a.can?(:edit, User.first)
=> false
However when I launch a web browser, log me in as a user and try to edit another one, CanCanCan remains silent.
If I replace can by cannot, I can't edit any user. It's as if it didn't lookup the condition.
My UsersController got this line on top
authorize_resource
I'm stuck with this, any help would be gladly appreciated.
cancancan 2.3.0
rails 5.2.1
Make sure that your instance (#user) is loaded before authorize_resource action runs, otherwise it will check if user can access some Users (can?(:edit, User), which is always true), instead of exact user.
before_action :load_user, except:[:index, :new, :create]
authorize_resource
...
private def load_user
#user = User.accessible_by(current_ability, action_name.to_sym).find(params[:id])
end
I am using the gem file acts_as_votable. Currently, when I click the "Like" button it seems that it is working, and the flash notice says "You have liked it!" However, I would like to keep track of how many likes I have for each review. So, I added this line of code in my index.html.erb file (below) but I am getting an error stating: "Could not find table 'votes' "
<td><%= link_to 'Like', like_review_path(review), method: :post%>
(<%=review.get_upvotes.size%>)</td>
This is what I have in my reviews_controller.rb file:
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :edit, :update, :destroy, :upvote]
def upvote
#review = Review.find(params[:id])
#review.upvote_by #current_user
flash[:notice] = 'You liked the review!'
redirect_to :back
end
This is for my routes.rb file:
resources :reviews do
member do
post "/like", to: "reviews#upvote"
end
end
Finally, this is what I have in my review.rb file:
class Review < ApplicationRecord
acts_as_votable
belongs_to :user
end
Everything seems to be correct on your code.
try to run the migrations, this seems to be the problem. when you run this
rails generate acts_as_votable:migration
you need to run the migrations too
rake db:migrate
check that the migrations are in the last version and if this doesn't work, restart the rails server, but if it says that could not find the table, then you just didn't runned the migrations, or they returned an error, or deleted the table manually after the migration was done. please see those cases so we can discard this options
Right let me explain a little in code, I'm trying to get the user to stay on the edit action until the user is completely valid. So far I have the following code in the application controller:
def check_privileges!
redirect_to "/users/edit" until current_user.valid?
end
registrations_controller.rb
before_filter :check_privileges!, only: [:new, :create]
jobs_controller.rb
before_filter :check_privileges!, only: [:index]
Now when I click on the link to jobs#index it gives me the following error. I cannot see a redirect in jobs#index
AbstractController::DoubleRenderError in JobsController#index
No clue how to sort this, I've tried and return but I cannot figure this out. I've been doing this all day as a user has to complete their profile before they have full access to the application.
Any clues, this is really bugging me now and I have no smart programming friends to help me.
I think what you want is unless not until... until will cause a loop of redirect_to (thus leading to multiple redirects) if the current_user is not valid.
Try:
def check_privileges!
redirect_to "/users/edit" unless current_user.valid?
end
Basically I have a UsersInitializeController Class
class UsersInitializeController < ApplicationController
before_filter :authenticate_user!
def create
render true
end
end
authenticate_user! is found in the Application Controller
class ApplicationController < ActionController::Base
# protect_from_forgery
def authenticate_user!
#current_user = User.find_by_token params[:auth_token]
if !#current_user
#current_user = User.create :token => params[:auth_token]
end
end
end
When my application starts, it sends POST request to the UsersInitializeController. Since before_filter is set, it will thus call authenticate_user! first. However the error I got says before_filter is an undefined method.
From my knowledge, before_filter exist in ActionController, and since UsersInitializeContoller < ApplicationController < ActionController, I shouldn't be getting this error. Has anyone encounter this issue before ?
Exception Stack (as requested)
Started POST "/users_initialize.json" for 127.0.0.1 at 2012-03-06 00:32:50 -0800
ActionController::RoutingError (undefined method `before_filter' for UsersInitializeController:Class):
app/controllers/users_initialize_controller.rb:3:in `<class:UsersInitializeController>'
app/controllers/users_initialize_controller.rb:1:in `<top (required)>'
Routes.rb file (as requested)
MyApplication::Application.routes.draw do
resources :users_initialize
match 'info/required_client_version' => 'info#required_client_version'
end
### Problem Solved ###
Unused Devise Gem somehow causing the complication. Removed it and done.
add the before_filter within an "included do" block:
included do
before_filter :authenticate_user!
end
Update:
just noticed you solved it already. However I was having the same troubles and the solution above solved it in my case. So, I'll leave the comment here since it may help others
Not able to reproduce, the code you posted works fine on my Rails 3.2.2 app.
Probably there is something wrong with your source file (i.e. some extra hidden bytes somewhere).
You could try a step-by-step approach to solve this:
add a new UsersController and add resources :users to routes.rb
add an index action with the following code:
def index
render :text => "Hello there"
end
When you visit http://localhost:3000 you should see the text "Hello there"
Add the before_filter and verify that the filter is executed by adding e.g. logger.warn( 'In the Filter' ) to the beginning of the filter method
I'm unable to use 'user_signed_in?' in my application controller, and wondered if anyone knew how to fix it.
It's works fine in my views, but in my application controller i get
NoMethodError in PostsController#index
undefined method `user_signed_in?' for ApplicationController:Class
A lot of people had this problem on rail 3.0.3, but I'm using rails 2.3.8. The suggested fix was
to use devise_for :user in your routes.rb but that resulted in
Internal Server Error
undefined method `devise_for' for main:Object
Help would be greatly appreciated
Thanks
I use devise with 2.38
How about having
==> application_controller.rb <==
protected
def authorize
unless User.find_by_id(session[:user_id])
session[:original_uri] = request.request_uri
flash[:notice] = "Please Log In!"
redirect_to :controller => 'admin', :action => 'login'
end
end
end
then each controller, e.g. food_items:
class FoodItemsController < ApplicationController
before_filter :authorize, :except => [:index, :show] # For all methods except these...
# GET /food_items
slightly different approach. Might help.