0
votes

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?

1
I think you meant to use f.text_field :first_name and f.text_field :last_name with your regular optionsMrYoshiji
@MrYoshiji And you're right...Man, talk about not seeing to big picture and focusing on small issues. Post an answer and I'll accept it.Francis Ouellet
It's okay, end of the day, also a warm day here in Montreal... I understand ;)MrYoshiji
A fellow Montrealer, nice to meet you. And I owe you a beer!Francis Ouellet
Sorry, I thought I had accepted it right after you posted it. My bad!Francis Ouellet

1 Answers

1
votes

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" %>
     #^^^^^^^^^^