0

I'm working with Rails 4 and Devise 3.0.0 and am new to using these new strong parameters. I added a first_name and last_name to the User model using a migration and then added the following to my application_controller.rb based on the Devise Wiki.

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  before_filter :configure_permitted_parameters, if: :devise_controller?
  protect_from_forgery with: :exception
  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation) }
  end
end

My sign_up view (/views/devise/registrations/new.html.erb) is blowing up with this error message:

undefined method `first_name' for #<ActionView::Helpers::FormBuilder:0x007fdcbcbe2170>

Here's my complete view:

<div class="container">
  <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>
    <h2 class="form-signin-heading">Sign up</h2>
    <%= f.first_name :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
    <%= f.last_name :last_name, :placeholder => 'List name', :class => "form-control" %>
    <%= f.email_field :email, :placeholder => 'Email address', :class=> "form-control"  %>
    <%= f.password_field :password, :placeholder=> 'Password', :class=> "form-control" %>
    <%= f.password_field :password_confirmation, :placeholder=> 'Password confirmation', :class=> "form-control" %>
    <%= f.submit "Sign up", :class => 'btn btn-lg btn-primary btn-block'  %>
    <div class="shared_links"><%= render "devise/shared/links" %></div>
  <% end %>
</div>

If I remove both :first_name and :last_name lines from the registration#new form everything works fine.

I've look at a few other posts with similar issue to no avail. Any ideas?

Community
  • 1
  • 1
Francis Ouellet
  • 505
  • 4
  • 15

1 Answers1

1

I think you meant to use f.text_field:

# This:
<%= f.first_name :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
<%= f.last_name :last_name, :placeholder => 'List name', :class => "form-control" %>

# Should become:
<%= f.text_field :first_name, :autofocus => true, :placeholder => 'Fist name', :class => "form-control" %>
     #^^^^^^^^^^
<%= f.text_field :last_name, :placeholder => 'List name', :class => "form-control" %>
     #^^^^^^^^^^
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117