Activeadmin 1.3.2 issues in rails 5.2.1 - ruby-on-rails

I am using the active admin gem into my new project with Ruby 2.5.1p57 and Rails 5.2.1. All are good. But, I am facing the issue with open new resource link or update the resource. Even, If I add permit_params, same issue.
Started GET "/admin/admin_users/new" for 127.0.0.1 at 2018-10-04 20:59:08 +0530
Processing by Admin::AdminUsersController#new as HTML
AdminUser Load (0.5ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 1 ORDER BY `admin_users`.`id` ASC LIMIT 1
↳ /home/vivek/.rvm/gems/ruby-2.5.1#regroup2/bundler/gems/activeadmin-
c301ab126b3f/lib/active_admin/base_controller.rb:39
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.5ms)
ArgumentError (wrong number of arguments (given 2, expected 0..1)):

But finally, Fixed the issue by adding below line code to the following
file:
config/initializers/active_admin.rb
def resource_params
[(params[resource_request_name] || params[resource_instance_name]).try(:permit!) || {}]
end
Not sure. Is this correct way to do.

Related

Devise redirect to on successful login seems to get stuck on first login attempt

I'm developing a small application using Ruby on Rails and using Devise for authentication. I've two login systems setup and one is user and the other one is employee
When logging in using correct email and password, devise sends the correct redirect to path back but it gets stuck there. Nothing continues unless I refresh the page. But once I refresh it, it will happily go to the redirect path and even if after logging out in the same browser tab and then logging back in works. But if I close the tab and then load the application in a new tab, it doesn't work.
Following is the rails server output when logging in,
Started POST "/employees/sign_in" for ::1 at 2020-06-21 18:21:50 +0530
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Pr0k+3+FfU72BcwTVRhWxQYBTD/zcv5+QBdWuovDRd4+yGxC1OBWmvAscWJbnog2vVTLpbPM2xOwVxGC3UsLRg==", "employee"=>{"email"=>"employee#example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
Employee Load (0.4ms) SELECT "employees".* FROM "employees" WHERE "employees"."email" = $1 ORDER BY "employees"."id" ASC LIMIT $2 [["email", "employee#example.com"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:23
CACHE Employee Load (0.0ms) SELECT "employees".* FROM "employees" WHERE "employees"."email" = $1 ORDER BY "employees"."id" ASC LIMIT $2 [["email", "employee#example.com"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:23
Redirected to http://localhost:3000/employee/dashboard
Completed 302 Found in 818ms (ActiveRecord: 0.4ms)
I'm not sure why this happens and hitting a dead end when trying to find anything related to this error.
Following is the code for my after_sign_in_path function
def after_sign_in_path_for(resource)
set_flash_message! :alert, :warn_pwned if resource.respond_to?(:pwned?) && resource.pwned?
set_root
# super
end
Here set_root function returns a String with the path for the redirect.
Version information
Ruby version - ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin19]
Rails version - Rails 5.2.4.2
Devise version - 4.7.1
Turbolinks - Yes
Try replacing your form_for with form_with in your sessions/new.html.erb. I have a similar problem (Rails 6) and it seems to be ok now.
<%= form_with(model: resource, scope: resource_name, url: session_path(resource_name)) do |f| %>
Additionally, I uncommented the Turbolinks section in config/initializers/devise.rb:
ActiveSupport.on_load(:devise_failure_app) do
include Turbolinks::Controller
end
This sounds like an issue with your frontend (more specifically Turbolinks) rather than the backend. As your log shows, the redirect actually happens from an HTTP standpoint.
Since I haven't run into this issue myself (mainly because I haven't used Turbolinks much in more complex apps), I can only point you to a few resources that describe issues similar to yours, namely:
Getting turbolinks wrapper to work with Ruby on Rails Devise
https://www.oarod.com/2017/01/29/turbolinks-with-devise/
I hope one of these two helps.
If not, feel free to share some frontend code – like I said, I think the issue is on the frontend, not the backend.
The first thing I would try is to do a return redirect_to :
def after_sign_in_path_for(resource)
set_flash_message! :alert, :warn_pwned if resource.respond_to?(:pwned?) && resource.pwned?
return redirect_to set_root
# super
end

Controller gives wrong number of arguments error when GET request is made

I have written a rails-api with two resources users and books. When I make a GET request for books it works properly, but for users it fails with the following message:
Started GET "/users/" for 127.0.0.1 at 2015-11-18 23:45:52 +0530
Processing by UsersController#index as HTML
User Load (0.5ms) SELECT `users`.* FROM `users`
Completed 500 Internal Server Error in 63ms (ActiveRecord: 4.2ms)
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/users_controller.rb:8:in `index'
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.erb (17.6ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.1ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /Users/me/.rvm/gems/ruby-2.2.3/bundler/gems/rails-35ca78a07c8b/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (69.4ms)
Here is the error I get on the browser:
ArgumentError in UsersController#index
wrong number of arguments (0 for 1)
Extracted source (around line #8):
...
# GET /users
def index
#users = User.all
render json: #users # this is line 8
end
...
config/routes.rb
Rails.application.routes.draw do
resources :users
resources :books
end
Any clue on what might be going wrong?
The error was with my model that was being passed to the serializer. I had a column in my database with the name method. method is a rails reserved word (read: ruby-and-rails-reserved-words) which was creating the problem. I renamed my column to registration_method which fixed the issue. Thanks everyone for the help :)
could try render #users.to_json
Is User.all returning any users?

devise_invitable not able to accept invitations anymore (but works via console)

I am using devise_invitable to create guest accounts to access a document. When accessing a document, a guest can click on an upgrade button that will upgrade to a full account. Clicking on the upgrade button is accepting devise_invitable invitation.
Was previously working: no longer working.
Environment:
rails 4.2.3 (recently upgraded)
devise 3.4.1
devise_invitable 1.3.6
I use a header partial for a guest user that generates an upgrade link:
<% #invite_link = accept_user_invitation_url(invitation_token: #invite_token) %>
<%= link_to "Upgrade", #invite_link, class: "btn btn-danger btn-lg" %>
#invite_token is created like:
user = current_user
user.invite! do |u|
u.skip_invitation = true
end
#invite_token = user.raw_invitation_token
So link_to helper generates something like: https://myapp/users/invitation/accept?invitation_token=vv_JqeFDLfyX65tx2KhR
Except when I click on the link, it takes me to: https://myapp/users/sign_in
With error message: The invitation token provided is not valid!
Log reports something like:
Started GET "/users/invitation/accept?invitation_token=vv_JqeFDLfyX65tx2KhR" for 31.15.32.222 at 2015-10-15 19:18:34 +1100
Processing by Devise::InvitationsController#edit as HTML
Parameters: {"invitation_token"=>"vv_JqeFDLfyX65tx2KhR"}
User Load (5.9ms) SELECT "users".* FROM "users" WHERE "users"."invitation_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["invitation_token", "ea9be9890c684e13ece13ec02f89bade81fcd3ac1fb982db72d8f8561d0cf289"]]
Redirected to https://myapp/
Filter chain halted as :resource_from_invitation_token rendered or redirected
Completed 302 Found in 53ms (ActiveRecord: 5.9ms)
Started GET "/" for 31.15.32.222 at 2015-10-15 19:49:52 +1100
Processing by PagesController#index as HTML
Redirected to https://myapp/users/sign_in
Completed 302 Found in 11ms (ActiveRecord: 0.0ms)
Am able to make this work via rails console.
If I run rails console in parallel, I am able to step through the above steps and generate an accept_invitation link that works as expected (i.e. it prompts user to set a password for the full account).
NB: the raw_invitation_token is different in the rails console session.
What am I missing?
I found code that called user.invite! to generate raw_invitation_token when page was displayed and after the accept_invitation link was generated. This meant that the token in the accept_invitation link was now outdated.
Going via the console, this code was not called as no controller, view or javascript components are invoked. So when using the console, things worked as expected.
Fixed and all back to normal.

rails hiredis undefined symbol

I want to build a shopping cart for my website. I've installed redis/hiredis but when I start the server, if I go on a page where the shopping cart whould show a value (current items in cart), or if I want to go on the shopping cart page, the server crashes giving the following information:
Started GET "/cart" for 127.0.0.1 at 2015-05-19 13:43:33 +0300
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by CartsController#show as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
/home/svuser/.rvm/rubies/ruby-2.2.0/bin/ruby: symbol lookup error: /home/svuser/.rvm/gems/ruby-2.2.0/gems/hiredis-0.4.5/lib/hiredis/ext/hiredis_ext.so: undefined symbol: rb_thread_select
In my index page, I create a link to the shopping cart using the following instructions :
<%if signed_in?%>
<li>
<%= link_to cart_path do%>
<i class="fi-shopping-cart"></i>
My Cart
<%end%>
Is there any way to work around this issue ? I am still new to rails and I couldn't figure it out on my own.
I faced a similar problem. In my case, I use Ruby 2.2.2p95 and old version gem hiredis - '0.4.5'. It uses method rb_thread_select, which was removed in Ruby 2.2
So bundle update hiredis helps me.

Controller action not executing

Environment:
Rails: 3.2.12
Ruby: ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
In my routes.rb, I have:
match '/attendance', to: "attendance#attendance_dashboard"
In my attendance_controller.rb, I have:
before_filter :authenticate_user!
before_filter :allow_only_if_admin
def attendance_dashboard
logger.info("File: #{__FILE__} -- Line: #{__LINE__}")
#registered_and_not_checked_in = get_registered_and_not_checked_in()
end
I am running on the test system, so logger.info should write to the test.log file.
When I run the /attendance action, I get 500 error, and here's what I see in the test.log file:
Started GET "/attendance" for 73.169.212.94 at 2015-02-06 15:11:06 +000
Processing by AttendanceController#attendance_dashboard as HTML
[1m[35mUser Load (0.4ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Completed 500 Internal Server Error in 215ms
I was expecting to have the logger info requested logged to the test.log file, but it's not there, so I'm failing prior to that. Any ideas?
Solution:
The issue was not with the code, it was that user #2, who is signed in as an admin, has a JSON record in his DB record that was failing to unpack properly. Once that was fixed the issue went away.

Resources