Night Train
Night Train

Reputation: 781

Adding Fields To Devise Sign Up Using Rails 4

To start off, I'm new to rails, so I'm still getting the hang of things.

I'm attempting to add two fields to the Devise sign up page, a first name field and a last name field. I've gone ahead and changed the view and the controller in order to attempt this. They look like this:

Controller:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_devise_permitted_parameters, if: :devise_controller?

  protected

  def configure_devise_permitted_parameters
    registration_params = [:firstName, :lastName, :email, :password, :password_confirmation]

    if params[:action] == 'update'
      devise_parameter_sanitizer.for(:account_update) { 
        |u| u.permit(registration_params << :current_password)
    }
    elsif params[:action] == 'create'
      devise_parameter_sanitizer.for(:sign_up) { 
        |u| u.permit(registration_params) 
      }
    end
  end
end

View:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.label :firstName %><br />
  <%= f.fName_field :firstName %></div>

  <div><%= f.label :lastName %><br />
  <%= f.lName_field :lastName %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>

However, I get this error when I go to view the page:

undefined method `fName_field' for #<ActionView::Helpers::FormBuilder:0x00000003c89940>

Where must I define those methods? or are they automatically defined somewhere else?

Thanks for any help.

Upvotes: 1

Views: 2822

Answers (4)

Piyush Chaudhary
Piyush Chaudhary

Reputation: 313

I know it's a very common mistake done by a rookie while creating a registration form on the very first time because the devise puts all of the fields like f.email_field,f.password_field etc but these all are valid controls, so the developer gets confuse to type an attribute with _field
The 'fname_field' you used to define your first name is not acceptable as the form controls.

Please verify all of the controls which can be used here

and as per the current scenario, you should go for f.text_field

Upvotes: 0

Nikita Singh
Nikita Singh

Reputation: 370

create a migration like this to add first name and last name to users table:-

class AddFirstnameAndLastnameToUsers < ActiveRecord::Migration
  def change
   add_column :users, :firstName, :string
   add_column :users, :lastName, :string
  end
end

And add this to your view :-

     <div><%= f.label :firstName %><br />
          <%= f.text_field :firstName%></div>
     <div><%= f.label :lastName %><br />
          <%= f.text_field :lastName %></div>

You can not do f. to column of the table, you do it on the type of the field.

Upvotes: 1

MarkoHiel
MarkoHiel

Reputation: 16301

With "f.something" you can just define the type of the field in the form. Try something like

<%= f.text_field :lastName %>

This should work.

You can read about the available types of fields here: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

Upvotes: 5

Richard Peck
Richard Peck

Reputation: 76774

undefined method `fName_field'

This means you don't have the fName column in your database


If you have access to your DB, you'll see that Devise produces a table with a bunch of columns inside. By default, these columns only include email & password for the user. To add more, you first need to add the column to the database & then you should be able to do what you're doing with it.

There's a good railscast on devise you can see how to do this

Upvotes: 0

Related Questions