undefined method `username' for #<AdminUser:0x00000004bb2e58> - devise

I first installed devise and altered it so I can login with username instead of email.
After that I entered the Activeadmin gem into my gem file.
After that I did a bundle install and rake active_admin:install.
But after trying to log into the backend /admin/login I see this error message :
undefined method `username' for #<AdminUser:0x00000004bb2e58>
On this code :
Extracted source (around line #7):
4: <% scope = Devise::Mapping.find_scope!(resource_name) %>
5: <%= active_admin_form_for(resource, :as => resource_name, :url => send(:"#{scope}_session_path"), :html => { :id => "session_new" }) do |f|
6: f.inputs do
7: resource.class.authentication_keys.each { |key| f.input key, :input_html => {:autofocus => true}}
8: f.input :password
9: f.input :remember_me, :label => t('active_admin.devise.login.remember_me'), :as => :boolean, :if => false #devise_mapping.rememberable? }
10: end
Anyone a idea how to solve this ?
If you need more info just ask.
Roelof
edit : I thought I could solve this by doing this: http://blog.blazingcloud.net/2012/07/29/activeadmin-with-existing-devise-authentication/
But now all the login are failed ( 401) where they without activeadmin they were successfull

I was having the same error and I solved it adding this to my AdminUser model
def login=(login)
#login = login
end
def login
#login || self.email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
where(conditions).where(["lower(email) = :value", { :value => login.strip.downcase }]).first
end

Related

How to display only the value in edit page in Active Admin

I have a edit form in Active Admin. I need some field as read only.
My current edit page is like
I need the page look like this
How can this be done. My code for the edit form page is like
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.input :current_address, :label => 'Current Address', :as => :string
end
end
Please help.
As Alex said, set to disabled. You could then use css to get the visual you wanted, if you can live with the semantics of that.
The syntax was slightly different for me to get this to work.
in your admin form:
f.input :finish_position, input_html: { disabled: true }
in your CSS active_admin.css
input[disabled="disabled"],
input[disabled] {
background-color: #F4F4F4;
border: 0px solid #F4F4F4 !important;
}
For a cleaner form definition within your ActiveAdmin.register{} block you may as well want to define a "readonly" input type to be used within active admin using formtastic:
Form block syntax is for activeadmin version 1.0.0.pre at 0becbef0918a.
# app/admin/inputs/readonly_input.rb
class ReadonlyInput < Formtastic::Inputs::StringInput
def to_html
input_wrapping do
label_html <<
template.content_tag('div', #object.send(method))
end
end
end
# app/admin/your_model.rb
ActiveAdmin.register YourModel do
# ...
form do |f|
# ...
input :current_address, as: :readonly
# ...
end
end
I was facing the same issue and tried using :disabled but it did not solve my problem as I wanted field value to be included in params object while sending it to the server. When you mark a form input as :input_html => {:disabled => true} , it does not include this field value in params.
So, instead I used :input_html => {:readonly => true} which solved both of my problems:
Does not allow user to edit
Includes the value in params
I hope this will help.
How about this?
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.li do
f.label :current_address
f.span f.object.current_address
end
end
end
Try to add , :disabled => true for address input field.
The trick is to use "object". Here is how you should code it:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Users" do
f.input :device, :label => 'Device', :as => :select, :collection => DEVICE, :include_blank => false
f.label :current_address, f.object.current_address
end
end

User signed out halfway through cucumber test

I'm in the process of splitting change-password functionality off into its own page on my site. The whole process works fine when I run through it manually as a real user, but I can't get my Cucumber tests to pass, the reason being that on the final call to the controller, the current_user is mysteriously returning nil.
I've having a serious WTF moment because the user DEFINITELY is being logged in at the beginning of the tests, and I'm following all the same steps as other passing tests, and as I said the whole thing works fine when stepping through it by hand.
Here's the test in question:
Scenario: The user should be able to change their password
Given I have a signed in user with email = "test#mycompany.com" and password = "password"
When I am on the change-password page # this hits registrations_controller#change_password
And I fill in "Current password" with "password"
And I fill in "New password" with "new_password"
And I fill in "Re-enter new password" with "new_password"
And I press "Update"
Then I should see "You updated your account successfully"
And the login step:
Given /^I have a signed in user with email\s*=\s*"([^"]*)" and password\s*=\s*"([^"]*)"$/ do |email, password|
#user = User.new
#user.email = email
#user.password = password
#user.confirm!
#user.save!
visit '/sign_in'
fill_in("Email", :with => #user.email)
fill_in("Password", :with => #user.password)
click_button("Sign in")
end
Controller actions, from "registrations_controller.rb":
def change_password
# calling "current_user" here retuns the logged-in user as expected
#user = current_user
end
def update_password
# calling "current_user" here returns nil
# error occurs at call to nil.update_with_password
#user = current_user
if #user.update_with_password( params["user"] )
redirect_to after_update_path_for( #user )
else
clean_up_passwords( #user )
render_with_scope :change_password
end
end
Now, we are using devise (1.1.8), and my best guess is that I've done something wrong with the devise routes, which look like this in "routes.rb"
devise_for :users, :controllers => {:confirmations => "confirmations", :sessions => "sessions", :registrations => "registrations"} do
# ...other routes here...
get "/change_password", :to => "registrations#change_password"
put "/update_password", :to => "registrations#update_password"
end
Finally, for completeness, here is "change_password.html.haml"
= render :partial => "shared/stats"
#main
= form_for(resource, :as => resource_name, :url => "update_password", :html => { :method => :put, :autocomplete => "off" }) do |f|
= devise_error_messages!
.edit-account-hold
%span Your password
= f.label :current_password, "Current password"
= f.password_field :current_password
= f.label :password, "New password"
= f.password_field :password
= f.label :password_confirmation, "Re-enter new password"
= f.password_field :password_confirmation
= f.submit "Update", :class => "orange-button border-radius"
= link_to "Cancel", account_path, :id => "cancel-link"
:javascript
$(document).ready( function() {
$("#user_current_password").focus();
});
Try running it through Selenium. Watching the test run can often give clues about what's going wrong.

Devise:: Cannot create user through rake db:seed (failure confirmation_instructions)

User model has a function
def self.createadmin(
User.create(:email => "abc#gmail.com", :password => "123456e", :password_confirmation => "123456e", :terms_of_service => '1')
end
In rake db:seed, I have to call User.createadmin
However, this fails
ActionView::Template::Error: ActionView::Template::Error
from /Users/bever/Projects/tr/app/views/devise/mailer/confirmation_instructions.html.erb:3:in `_app_views_devise_mailer_confirmation_instructions_html_erb___1974818942364630283_2154906860'
Then I changed the code in createadmin
begin
User.create(:email => "abc#gmail.com", :password => "123456e", :password_confirmation => "123456e", :terms_of_service => '1')
rescue => e
User.create(:email => "abc#gmail.com", :password => "123456e", :password_confirmation => "123456e", :terms_of_service => '1')
end
It works! Any clue why this is happening?
Have you tried seeding from the db/seeds.rb file instead of the model? When you try to do it on the model, devise is probably trying to send the conformation mail.
You should create your admin user on the seeds.rb file like this
User.create(:email => "abc#gmail.com", :password => "123456e", :password_confirmation => "123456e", :terms_of_service => '1')
Remember that if you are using confirmable module of devise you should add this field to the query.
:confirmed_at => Time.now
Maybe you should add the confirmation tokens and other fields useful to administrate your admin account via your rails app and not on the console.
PD: Probably if you post more of the error shown and maybe the line in the view I can help you more.
Greetings

Rails 3 Route error - "No Route Matches"

I've been reading over this resource as well as this post to try to understand Routes more (currently learning programming/Rails by doing) but am wondering how I can fix the error I'm getting, which is No route matches {:controller=>"profiles", :action=>"show"}.
I get the error working my way through a Rails 3 sign-up process using nested model forms. The sign-up process, as follows:
user = User.new
user.email = ""
user.password = ""
user.profile = Profile.new
user.profile.save
user.save
The sign-up process starts at the homepage with the following form:
<%= form_for :user, :url => signup_path, :html => {:id => 'homepage'} do |f| %>
<div>
...
</div>
<%= f.fields_for :profile do |f| %>
<% end %>
<% end %>
Then the process goes to fill in the profile, then redirect to the new User's profile after this form is completed:
<%= form_for :profile, :html => { :multipart => true } do |f| %>
<div>
...
</div>
<%= f.fields_for :user do |f| %>
<% end %>
<% end %>
I have accepts_nested_attributes_for :user and :profile in their respective models.
My rails server it gives me a bit more detail:
ActionController::RoutingError (No route matches {:controller=>"profile.save", :action=>"show"}):
app/controllers/profiles_controller.rb:15:in `create'
So in my ProfilesController in 'create':
def create
#profile = Profile.new(params[:profile])
if #profile.save
redirect_to profile_path, :notice => 'User successfully added.'
else
render :action => 'new'
end
end
Seems clear that the issue is in profile_path, so my Routes.rb:
post "/signup" => "profiles#create", :as => "signup"
match "skip/signup", :to => "info#signupskip"
match "skip/profiles/new", :to => "profiles#newskip"
root :to => "users#create"
Can anyone help shed light on what I'm doing wrong/missing in my Routes.rb file?
The redirect path should contain the specific profile to redirect to:
if #profile.save
redirect_to profile_path(#profile), :notice => 'User successfully added.'
else
.....
Also the routes should include this line:
get "/profiles/:id" => "profiles#show", as => "profile"

Form_tag remote in Rails 3 in partial

To become member, A user can use a button to create a membership on the community page. This is made on a partial that create a new "membership"
the memberships_controller :
#community = Community.find(params[:community_id])
#community.memberships.create(:user => current_user, :role => 1)
in The view :
<% form_remote_tag :url => community_memberships_path(#community) do %>
<%= submit_tag 'Join' %>
<% end %>
After upgrading to Rails 3, that doesn't work anymore !
I tried this :
<% form_tag( {:url => community_memberships_path(#community)}, :remote => true) do %>
and this :
<% form_tag :url => {:controller => '/memberships/new', :action => :create,:community_id => #community }, :remote => true do %>
But no chance.. and have this error
No route matches "/communities/2
Thx for help
The URL is the first argument, options are secondary.
<% form_tag(community_memberships_path(#community), :remote => true) do %>
...
As for your No route matches exception, check you have translated you routes to the Rails 3 syntax correctly.