Problem adding a custom field to Devise while testing with cucumber - ruby-on-rails

I added a username field to my Rails app that's using Devise. I did all the necessary steps like adding the field to the views, adding it to the User model, migrating my database, etc. Now, when I run through the registration steps manually everything works fine. But as soon as I try to write a feature in Cucumber I get a huge page of errors.
Here's a very short list of the errors I get when running my cucumber feature
Scenario: New user registration with valid info # features/user_access.feature:7
When I sign up as a new user with valid info # features/step_definitions/user_access_steps.rb:5
undefined method `username' for #<User:0x00000102f7dcf0> (ActionView::Template::Error)
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/actionpack-3.0.3/lib/action_view/helpers/form_helper.rb:1019:in `value_before_type_cast'
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/actionpack-3.0.3/lib/action_view/helpers/form_helper.rb:1007:in `value_before_type_cast'
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/actionpack-3.0.3/lib/action_view/helpers/form_helper.rb:915:in `block in to_input_field_tag'
/Users/Daniel/.rvm/gems/ruby-1.9.2-p136#Flow1/gems/actionpack-3.0.3/lib/action_view/helpers/form_helper.rb:915:in `fetch'
I have isolated the error to my Devise view. For some reason cucumber doesn't like the following lines
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! % >
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
For some reason cucumber wont play nice with the :username label and text_field. All I am doing in my cucumber steps is going to the registration page and trying to fill in username with a name.
Any idea what is causing this error and how to fix it?

Have you updated the test database? rake db:test:prepare

Related

uninitialized constant Formtastic::I18n::SCOPES

Rails 6.0.1, Puma 4.3.0, Devise 4.7.1, ActiveAdmin 2.4.0, Formtastic 3.1.5.
Once again I am creating a Rails application. Created the foundation. A couple of models. Made a simple front-end for them. Next install Devise, ActiveAdmin.
In general, nothing unusual. Locally everything works fine.
But on the server, some kind of nonsense began to happen with ActiveAdmin.
In general, the application on the server is working fine. Through the console, I can create all the data. These data are successfully displayed on the site.
But if I go into ActiveAdmin...
Dashboard page displayed successfully. But if I go to the index page of any entity, then I will get the following error (from the log):
2019-11-19T00:55:04.216309411Z app[web.1]: web| I, [2019-11-19T00:55:04.198819 #14] INFO -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7] Rendered vendor/bundle/ruby/2.5.0/gems/activeadmin-2.4.0/app/views/active_admin/resource/index.html.arb (Duration: 73.2ms | Allocations: 17729)
2019-11-19T00:55:04.216369240Z app[web.1]: web| I, [2019-11-19T00:55:04.199305 #14] INFO -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7] Completed 500 Internal Server Error in 86ms (ActiveRecord: 5.8ms | Allocations: 19675)
2019-11-19T00:55:04.216376506Z app[web.1]: web| F, [2019-11-19T00:55:04.203841 #14] FATAL -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7]
2019-11-19T00:55:04.216380926Z app[web.1]: web| [9ac98910-3a38-496d-9f5e-f276bc590ba7] ActionView::Template::Error (uninitialized constant Formtastic::I18n::SCOPES
2019-11-19T00:55:04.216385058Z app[web.1]: web| Did you mean? Sprockets):
2019-11-19T00:55:04.216388027Z app[web.1]: web| [9ac98910-3a38-496d-9f5e-f276bc590ba7] 1: insert_tag renderer_for(:index)
The entire log: https://pastebin.com/raw/buWSveBZ
Only one section for entities works - this is the show action. Only two actions for entities work - these are show and destroy. Everyone else catches the error that I showed above.
I absolutely don't understand what's the matter.
I successfully use the identical config/initializers/active_admin.rb file in two other Rails 6 applications. Below I will show an example of one of the file for ActiveAdmin:
# frozen_string_literal: true
ActiveAdmin.register User do
menu priority: 5
permit_params :email, :full_name, :roles, :password, :password_confirmation
remove_filter :users_roles
controller do
def find_resource
scoped_collection.find_by!(pkey: params[:id])
end
end
index do
selectable_column
id_column
column :pkey
column :email
column :full_name
column :roles
column :current_sign_in_at
column :sign_in_count
column :created_at
actions
end
form do |f|
f.inputs do
f.input :email
f.input :full_name
f.input :roles
f.input :password
f.input :password_confirmation
end
f.actions
end
end
I'm hope for your help.
For what it's worth, I have a rails 6 app that I hadn't touched in about a year and was getting a similar error despite never having an issue with my other rails app that I use regularly -- uninitialized constant Formtastic::Util
Commenter above's advice of running rails generate formtastic:install did the trick for me. Thanks!

Rails 5.2.3 Blob Error with Active Storage when create file

I'm using rails 5.2.3 and I am trying to use ActiveStorage for a new scaffolded agenda. I have followed this tutorial
I want to save files locally on the filesystem.
At the moment when I want to create a new object by web form, I get the following error:
Unable to autoload constant ActiveStorage::Blob::Analyzable, expected
/home/vagrant/.rvm/gems/ruby-2.6.2/gems/activestorage-5.2.3/app/models/active_storage/blob/analyzable.rb
to define it
From Mail-log:
[ERROR] blogs create (NoMethodError) "Cannot load Rails.config.active_storage.service:\nundefined method `fetch' for n
And when I refresh the page I get:
undefined method `fetch' for nil:NilClass
My Definitions
model:
has_one_attached :image
controller:
def create
#blog = Blog.new(blog_params)
.
.
end
def blog_params
params.require(:blog).permit(:name, :audit_comment, :image)
end
views:
= simple_form_for(blog) do |f|
= f.error_notification
.form-inputs
= f.input :name, label: 'Name'
= f.file_field :image
= f.input :audit_comment
.form-actions
= f.button :submit
In development.rb:
config.active_storage.service = :local
What have I missed?
I've runned rails active_storage:install and migrations as this setup suggested
When I whole new rails app it works fine.

Submitting multipart form leads to internal server error on Rails 4.2

When submitting a simple multipart form, the server crashes and displays "Internal server error".
I have scaled down the form to the minimal:
<%= form_for #media_object, :html => {:multipart => true} do |f| %>
<%= f.file_field :media_object_image %>
<%= f.submit "Create!" %>
<% end %>
The log trace:
ERROR ArgumentError: unexpected prefix: {"RackMultipart"=>nil, ""=>nil}
/Users/christer/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tmpdir.rb:111:in `make_tmpname'
/Users/christer/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tmpdir.rb:129:in `create'
/Users/christer/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tempfile.rb:132:in `initialize'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:22:in `new'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:22:in `block in create'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:135:in `get_current_head_and_filename_and_content_type_and_name_and_body'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:59:in `block in parse'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:56:in `loop'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart/parser.rb:56:in `parse'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/multipart.rb:25:in `parse_multipart'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/request.rb:375:in `parse_multipart'
/Users/christer/.rvm/gems/ruby-2.3.0#teamhubs4/gems/rack-1.6.4/lib/rack/request.rb:207:in `POST'
Googling around turns up multipart submission problems with Rails 4.2 due to maximum number of open multipart files, but this is definitely different.
I'm running Rails 4.2.6 with Ruby 2.3.
Although I'm not using the imgur gem, Phil Ross put me on the right track. I do indeed extend the Array class with a to_hash method. Removing this extensions, and voila!, things work as they should.
imgur is not alone. dropbox gem is also known to cause this. Check your Gemfile.

How do I create a new user in Devise during my migration

I had this working in an old project, but it's possible this changed in one of the more recent versions. I'm currently using Devise 2.0.4. I'm attempting to create a new user during my migration using
User.create :email => '[password]',
:password => '[password]',
:password_confirmation => '[password]'
but when I do this, it aborts with the following error
rake aborted!
An error has occurred, this and all later migrations canceled:
ActionView::Template::Error
Tasks: TOP => db:migrate:reset => db:migrate
(See full trace by running task with --trace)
Any help on this would be greatly appreciated!
Alternatively, I could create the user using the rails shell, but for consistency, I'd like to have one default user to get started every time.
Based on prasvin's comment, I found that the better way to do this was to go into db/seeds.rb and populate a seed element such as
User.create(:email => '[email]', :password => '[password]', :password_confirmation => '[password]')
This itself lead to a different error message
rake aborted!
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
Which I eventually found through a different post was related to the fact that in my config/environments/development.rb file I had not yet set
config.action_mailer.default_url_options = { :host => 'localhost:3000'}
Which means that when Devise was trying to send out the confirmation email, it failed because it didn't know what address to tell them to come back to. This is what caused the ActionView::Template::Error. Once that is all done, it works perfectly as expected.

Wrong Number of Arguments (3 for 2) when using Devise Authentication Gem

I did some research and I think the error is due to use of different argument conventions in Rails 3.1 as opposed to 3.0. How exactly should I go about fixing this?
wrong number of arguments (3 for 2)
Extracted source (around line #3):
1: <h2>Sign in</h2>
2:
3: <%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
4: <p><%= f.label :email %></p>
5: <p><%= f.text_field :email %></p>
6:
The full trace is here
This occurred while I tried to look up "/users/sign_in" with using the Devise gem. If this is unfixable how the heck would I downgrade my rails to 3.0? I tried changing the rails version in the gem file to 3.0.4 but it just threw me an error that my "activesupport" could not find a proper match.
You cannot just change the rails version and think its done. In fact, the Devise has released 2.0.0. Try that if you feel thats some bug. I am myself using Rails 3.1 and i am not facing any issues as such.

Resources