RailsApps / rails_layout

Generates Rails application layout files for various front-end frameworks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Devise layout generator adds non-existant name field to form

stuarthannig opened this issue · comments

I installed the Devise gem and then ran:

$ rails generate devise User
$ rake db:migrate
$ rails g devise:views
$ rails g layout:devise bootstrap3
$ rails g layout:navigation --force
$ rails s

When I clicked my way to Sign Up I received an error on the form for f.name

Looking at the migrations and schema, Devise does not create a name field.

I deleted

<div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, :autofocus => true, class: 'form-control' %>
</div>

from the /registration/new and /registration/edit view files and everything worked. Does Devise install the name fields by default for anyone else, or is the Rails Layout gem adding non-default fields to the view templates?

https://github.com/RailsApps/rails_layout/blob/master/lib/generators/layout/devise/templates/registrations/new.html.erb
Lines 5 - 8

I'd be happy to submit a patch.

edit: Forgot to mention I am using Ruby 2, Rails 4, and latest Devise gem.

Thanks for catching that. It's my oversight. The rails-devise example app I'm building adds a 'name' field, as an example of adding fields to the user model. For generic use of the Devise layout generator, there should be no 'name' field. Ideally, the Devise layout generator should check the User model and only add the extra code for the name field if it is present in the User model.

If you'd like to tackle this with a patch, it'd be great. Otherwise, I will add it to my to-do list.

I'll see what I can do. :)

Great! Thanks for offering to help. I'm not sure what's the best way to check to see if the User model contains a 'name' attribute. Either query the User model programmatically or parse the file?

I forgot to mention that the best way I could find to check for the name field on the User model is the following statements.

User.column_names.include? 'name'

or

User.method_defined? 'name'

Released version 1.0.2 with these changes.