Issue retrieving current_user in Devise with different model name - ruby-on-rails-3

I have got Devise setup in my rails app with my model called auth_user instead of user. I have tried accessing the currently logged in user's email address (a field called email) for me to use in a query against an Employee model like so
#employee = Employee.find_by_Email(current_auth_user.email)
but get the error:
undefined method `email' for nil:NilClass
Any ideas on how to resolve this issue?
Thanks
Paul
** EDIT **
Fix as suggested by #theButler in the comments. Resolution was to add
before_filter :authenticate_auth_user!
To the top of the class.

Related

Show User Profile Devise Rails

I am trying to allow users profiles to be viewed. I am using devise and have followed Creating a Users show page using Devise
Currently has a route of '/users/1' with 1 being the id of the user. I would like to make it '/users/username'.
I tried to implement this by doing:
"config/routes.rb"
match '/users/:username', to: 'users#show', via: 'get'
"app/controllers/users_controller.rb"
def show
#user = User.find(params[:username])
end
Even with this ^^ the route is still 'users/1'
Use FriendlyId, it is easy to use.
https://github.com/norman/friendly_id
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

Track model changes on virtual attributes with previous_changes

I'm tracking changes on my model and I would like to track changes on a has_many relation. The model has:
define_attribute_method :tag_tokens
attr_reader :tag_tokens
def tag_tokens=(ids)
tag_tokens_will_change! unless ids == #tag_tokens
self.tag_ids = ids.split(",")
end
Combination of http://railscasts.com/episodes/258-token-fields and http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
The result is this exception which is caused on the define_attribute_method line. I believe this method is required to create the tag_tokens_will_change method.
ruby-1.9.3-p194#mm/gems/activerecord-3.2.8/lib/active_record/attribute_methods/time_zone_conversion.rb:59:in `create_time_zone_conversion_attribute?': undefined method `type' for nil:NilClass (NoMethodError)
How can I get this to work?
I received the same error, and it turns out the documentation we were referring to is outdated.
The current way to trigger this change is to call attribute_will_change! :attr_name
See the accepted answer here for more details.

Active admin change default model admin_user

I'm starting my first project with Active Admin.
To use another model for my users I use the following command :
rails generate active_admin:install User
After this I make this change in active_admin initializer :
config.authentication_method = :authenticate_user!
config.current_user_method = :current_user
I'm correctly login my application but on the home page I get this error :
undefined method `destroy_admin_user_session_path' for #<ActiveAdmin::Views::HeaderRenderer:0x007ff8fa086a60>
How can I fix it properly ?
Solved by editing initializer :
config.logout_link_path = :destroy_user_session_path
This is addition to #Awea answer. Use togather with that.
Check rails routing table for destroy_user_session.
For example devise token auth make route table entry like this:
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
But default method for activeadmin logout link is :get and it will not work.
To make it worked properly add to config/initializers/active_admin.rb also and:
config.logout_link_method = :delete

how do i define a method in rails

I have generated scaffold for a author and I want to add an email address to the table.
I have added author.email in my tags and it wants me to define a method. what method should i define for email and where? is it in the authors controller.
thanks
In your case email is a field for the author. You should add it to your database migration, rerun the rake db:migrare and it'll be accessible as author.email
Also, make sure to add :email to the attr_accessible list in your author model.
I think you got it incorrectly. As i believe you receiving error call " undefined method or variable call email" isn't it??
If so that means you need to add email field to the database table table you can do that by following commands
ruby script/generate migration AddEmailToAuthor email:string
rake db:migrate
now you can run your program

Testing member routes of a resources in rails3 with rspec

I'm creating an application that will allow the user to take an exam. The exam object has_many questions and I want the user to take the exam in two parts. In order to implement this workflow, I've created the following routes in config/routes.rb:
resources :exam do
member do
get 'get_part1'
put 'put_part1'
get 'get_part2'
put 'put_part2'
end
end
So, when the user issues GET /exam/:id/get_part1 they are shown the first set of questions, etc... I have all of this working and now I'm trying to write tests for it - I know that's backwards but it took me a while to figure out complex forms and stuff. I want to test that you cannot access the exam controller unless you are a signed in user. This is straight forward for new and create but I'm having trouble figuring out how to test the nested members. Here's what I've tried so far:
before(:each) do
#exam = Exam.create
end
it "should deny access to 'get_part1'" do
get get_part1_exam_path(#exam)
response.should redirect_to(signin_path)
end
However, this test fails with the following error:
Failure/Error: get get_part1_exam_path(#exam)
ActionController::RoutingError:
No route matches {:controller=>"exams", :action=>"/exams/1/get_part1"}
Any help would be greatly appreciated. Thanks!
Try that one:
get :get_part1, :id => #exam (or #exam.id)