Pass a foreign table in a "form_for" - ruby-on-rails

I have two model
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :personal
before_create :generate_profile
end
and personal.rb
class Personal < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
end
and in my controller i have :
def createinfo
#user = current_user
#personal = #user.personal
end
I want to do a form for like that :
<%= form_for #personal do |f| %>
Trigramme : <%= f.text_field :trigramme, :input_html => { :value => 'blabla' } %><br />
Nom : <%= f.text_field :nom %><br />
Prenom : <%= f.text_field :prenom %><br />
Poste : <%= f.text_field :poste %><br />
Bio : <%= f.text_area :bio %><br />
<%= f.submit %>
<% end %>
But i have an error and I don't know how to resove that. I think the path current_user.personal is not accepted by form_for although it print the correct thing when I only put current_user.personal.trigramme in the code.
undefined method `personal_path' for #<#<Class:0x8e15a28>:0x8e15308>
do you know how can I do this form?
EDIT : My route.rb
Rails.application.routes.draw do
devise_for :users
root 'static_pages#cvpage'
get 'home' => 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'cvpage' => 'static_pages#cvpage'
get 'createinfo' => 'static_pages#createinfo'
end
EDIT 2 schema.rb :
create_table "personals", force: :cascade do |t|
t.string "trigramme"
t.string "nom"
t.string "prenom"
t.string "poste"
t.text "bio"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "personals", ["user_id"], name: "index_personals_on_user_id"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

YEAHH! I solve my problem (take a lot of time)
I put in my personal controller this :
def update
#user = current_user
#personal = #user.personal
if #personal.update_attributes(personal_params)
# if update is successful
else
# if update is unsuccessful
end
redirect_to home_path
end
def personal_params
params.require(:personal).permit(:trigramme, :nom, :prenom, :poste, :bio)
end
And i put in my view this form :
<%= form_for(#personal, html: { method: :put }) do |f| %>
Trigramme : <%= f.text_field :trigramme %><br />
Nom : <%= f.text_field :nom %><br />
Prenom : <%= f.text_field :prenom %><br />
Poste : <%= f.text_field :poste %><br />
Bio : <%= f.text_area :bio %><br />
<%= f.submit %>
Thank you to all of you !!

Related

Rails 5 - Nested Model Form Not Saving Child Model Attributes

I've been at this problem for a couple weeks now on and off.
Currently I have a User model, Employee model and an Employee_Contact model. The User model was generated using Devise. After the User is logged in they can go to their User Dashboard and create an Employee using a nested form. I am having an issue where the child model Employee_Contact is not saving into the database.
Schema:
create_table "employee_contacts", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "state"
t.string "zip"
t.string "phone_num"
t.string "alt_phone_num"
t.string "email"
t.string "emergency_contact_first_name"
t.string "emergency_contact_last_name"
t.string "emergency_contact_num"
t.integer "user_id"
t.integer "employee_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["employee_id"], name: "index_employee_contacts_on_employee_id"
t.index ["user_id"], name: "index_employee_contacts_on_user_id"
end
create_table "employees", force: :cascade do |t|
t.string "first_name"
t.string "middle"
t.string "last_name"
t.string "sex"
t.string "race"
t.date "birthdate"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "employee_contact_id"
t.index ["user_id"], name: "index_employees_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name:
"index_users_on_reset_password_token", unique: true
end
end
These are my models:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :employees, :dependent => :destroy
end
class Employee < ApplicationRecord
belongs_to :user
has_one :employee_contact, inverse_of: :employee, autosave: true
accepts_nested_attributes_for :employee_contact, allow_destroy: true
end
class EmployeeContact < Employee
belongs_to :employee, inverse_of: :employee_contact
end
My Employee Controller:
class EmployeesController < ApplicationController
before_action :authenticate_user!
def index
#employees = Employee.all
end
def new
#employee = Employee.new
end
def create
#employee = current_user.employees.create(employee_params)
if #employee.save!
redirect_to root_path
else
render :new, status: :unprocessable_entity
end
end
private
def employee_params
params.require(:employee).permit(:first_name, :middle, :last_name,
:sex, :race, :birthdate, employee_contact: [:id, :street, :city,
:state, :zip, :phone_num, :email, :alt_phone_num,
:emergency_contact_first_name, :emergency_contact_last_name,
:emergency_contact_num])
end
end
My View Form:
<%= form_with(model: #employee, local: true) do |form| %>
<h3>Employee Profile Information</h3>
<%= form.label :first_name %><br />
<%= form.text_field :first_name %><br />
<%= form.label :middle %><br />
<%= form.text_field :middle %><br />
<%= form.label :last_name %><br />
<%= form.text_field :last_name %><br />
<%= form.label :sex %><br />
<%= form.text_field :sex %><br />
<%= form.label :race %><br />
<%= form.text_field :race %><br />
<%= form.label :birthdate %><br />
<%= form.date_field :birthdate %><br />
<h3>Employee Contact Information</h3><br />
<%= form.fields_for :employee_contacts do |builder| %>
<%= builder.label :street %><br />
<%= builder.text_area :street %><br />
<%= builder.label :city %><br />
<%= builder.text_area :city %><br />
<%= builder.label :street %><br />
<%= builder.text_area :street %><br />
<%= builder.label :state %><br />
<%= builder.text_area :state %><br />
<%= builder.label :zip %><br />
<%= builder.text_area :zip %><br />
<%= builder.label :phone_num %><br />
<%= builder.text_area :phone_num %><br />
<%= builder.label :alt_phone_num %><br />
<%= builder.text_area :alt_phone_num %><br />
<%= builder.label :email %><br />
<%= builder.text_area :email %><br />
<%= builder.label :emergency_contact_first_name %><br />
<%= builder.text_area :emergency_contact_first_name %><br />
<%= builder.label :emergency_contact_last_name %><br />
<%= builder.text_area :emergency_contact_last_name %><br />
<%= builder.label :emergency_contact_num %><br />
<%= builder.text_area :emergency_contact_num %><br />
<% end %>
<%= form.submit "Add", class: 'btn btn-primary' %><br />
<% end %>
When I get submit the form to create a new employee this is what I get in my console parameters:
Processing by EmployeesController#create as HTML
Parameters:{"utf8"=>"✓","authenticity_token"=>"IgYzybxM8+M8jZ8NJgYKZ+Zhqr07kcV1e0QyWf> uY8hQLyBY844jfOlLNl65F/2RRr4TTW0F1MeSmDzVzBsXOjg==", "employee"=>
{"first_name"=>"Firt Nam", "middle"=>"kk", "last_name"=>"jak", "sex"=>"dfkj", "race"=>"jkadflj", "birthdate"=>"2011-01-01", "employee_contacts"=>{"street"=>"afdjkljadfkl", "city"=>"jadklfjakldfj", "state"=>"fadjlkdf", "zip"=>"32787", "phone_num"=>"567567567", "alt_phone_num"=>"57875875875", "email"=>"dajkjdlkfj#jkj.com", "emergency_contact_first_name"=>"adfjkladjf", "emergency_contact_last_name"=>"adfjkaldf", "emergency_contact_num"=>"784339793"}}, "commit"=>"Add"}
Unpermitted parameter: :employee_contacts
And so it saves the Employee but not anything associated with their contact information. If I run Employee.last in the Rails console I get a nil value for employee_contact_id. If I try and use the Rails Console to look up Employee_Contact.last or Employee_Contact.all I get an 'unable to autoload constant Employee_Contact, expected employee_contact.rb to define it'.
UPDATE
I changed a few things thanks to Anton.
My Employee Contact model now inherits from ApplicationRecord:
class EmployeeContact < ApplicationRecord
belongs_to :employee, inverse_of: :employee_contact
end
I changed the view form in the employee_contact loop from plural to singular:
<%= form.fields_for :employee_contact do |builder| %>
I added a build method in Employee#New
def new
#employee = Employee.new
#employee.build_employee_contact
end
And edited Employee#Create
def create
#employee = current_user.employees.create(employee_params)
if #employee.valid?
redirect_to root_path
else
render :new, status: :unprocessable_entity
end
end
This is now saving an Employee Contact. Here are the results from the console:
#<EmployeeContact id: 1, street: "dafjkafdjl", city: "adjfklajdk", state: "dfjkljalkdfjioad", zip: "dafjadlkfjkajdkl", phone_num: "adf", alt_phone_num: "871274892174", email: "dafnajdklj#jkklda.com", emergency_contact_first_name: "ajdfjalkj", emergency_contact_last_name: "fjadkljdfkl", emergency_contact_num: "11287244", user_id: nil, employee_id: 1, created_at: "2017-10-03 19:01:55", updated_at: "2017-10-03 19:01:55">
Employee id: 1, first_name: "dafad", middle: "dfad", last_name: "dafd", sex: "adfd", race: "dfad", birthdate: "1210-01-01", user_id: 1, created_at: "2017-10-03 19:01:55", updated_at: "2017-10-03 19:01:55", employee_contact_id: nil>
The Employee model is still getting an Employee_Contact_Id: nil value. However, the Employee Contact model is getting the correct Employee_Id attribute.
SOLVED
So I'm going to leave it at with what I have posted in the Update. There is no need for my Employee model to have an Employee_Contact_Id attribute. When I create a new Employee the Contact correctly maps to that Employee currently.
Please set the EmployeeContact to inherit from ApplicationRecord, not Employee.
In the new action build the employee contact for the nested form from the employee.
I would suggest to remove the foreign key from the has_one association.
The employee_id on the employee_contact is enough.

Rails: add relationship with other table for Devise user

I want to add an input to my Devise user sign_up form in order to add a team name. I created a specific table for Teams and I created a relationship: Users belong to Team. Team has many users.
The problem is I need to add a Team_id when submiting the sign_up form. How can I save the value of the Team input in the register form to the Team table and get the Id in order to save it to the team_id entry in Devise User table?
My Sign Up form:
<div class="container_index">
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :team %><br />
<%= f.text_field :team %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
My user schema:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "team_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["team_id"], name: "index_users_on_team_id"
end
My application controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password, :team) }
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :password, :current_password, :team) }
end
Thank you.

Rails update not saving values

Please help me. I've been tearing out my hair trying to figure out what I feel should be a simple thing.
My update function "succeeds" but it does not actually save the new values. The console log doesn't throw an error, but it does say "Unpermitted parameter: profiles"
edit.html.erb
<div class="col-md-7 col-md-offset-3 main">
<% provide(:title, "Edit user")%>
<center><h1>Update your profile</h1></center>
<%= form_for(#person) do |f| %>
<%= render 'shared/error_messages' %>
<div class="col-md-12">
<%= render 'layouts/profilefields', f: f %>
<%= f.submit "Save Changes", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
_profilefields.html.erb
<%= f.fields_for :profiles do |prf|%>
<!--
<% if !#profileInfo["avatar"].blank? %>
<%= image_tag #contactInfo.avatar_url(:medium).to_s, :class=>"profilePhoto" %>
<% end %>
<div class="photoPreview">
<i class="fa fa-upload photoUpload"></i>
<p id="uploadClick">Click to Upload</p>
</div>
<%= prf.file_field :avatar, accept: 'image/png,image/gif,image/jpeg, image/jpg', id: 'uploadAvatar' %>
<p class="deletePhoto">Delete</p>
-->
<%= prf.label :about %>
<%= prf.text_field :about, :class => "form-control" %>
<%= prf.label :why %>
<%= prf.text_field :why, :class => "form-control" %>
<%= prf.label :goals %>
<%= prf.text_field :goals, :class => "form-control" %>
<%= prf.hidden_field :traveler_id, value: current_traveler.id %>
<% end %>
travelers_controller.rb
class TravelersController < ApplicationController
def edit
#person = Traveler.find(params[:id])
#profileInfo = Profile.find_or_initialize_by(traveler_id: params[:id])
##profileInfo[:email] = current_traveler.email
#This builds the form
#person.build_profile(#profileInfo.attributes)
end
def show
end
def update
#trav = Traveler.find(params[:id])
#prof = Profile.find_or_create_by(traveler_id: current_traveler.id)
if #trav.update(update_traveler_params)
flash[:success] = "Profile updated"
redirect_to feed_path
else # Failed. Re-render the page as unsucessful
render :edit
end
end
private
def update_traveler_params
params.require(:traveler).permit(:id, profiles_attributes: [:id, :first_name, :last_name, :about, :why, :goals,
:avatar, :traveler_id])
end
end
and the models
class Profile < ApplicationRecord
belongs_to :traveler, foreign_key: "traveler_id"
end
class Traveler < ApplicationRecord
# Include default devise modules. Others available are:
# , :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one :profile
accepts_nested_attributes_for :profile, update_only: true, allow_destroy: true
end
and the schema. Is the profile foreignkey set up correctly?
ActiveRecord::Schema.define(version: 20160825224710) do
create_table "profiles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "first_name"
t.string "last_name"
t.text "about", limit: 65535
t.text "why", limit: 65535
t.text "goals", limit: 65535
t.string "avatar"
t.integer "traveler_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["traveler_id"], name: "index_profiles_on_traveler_id", using: :btree
end
create_table "travelers", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
end
I believe the problem is caused by your use of "profiles", when your association is actually a has_one.
Try changing:
profiles_attributes to profile_attributes in travellers_controller#update_traveler_params
:profiles to :profile in _profilefields.html.erb

Error adding value names to devise

Im getting an error trying to add the value name to devise, I think all did all things to make it work, but it doesnt work.
NoMethodError in Devise/registrations#new
undefined method `name' for #<User:0x00000002e4e038>
Here is my user model:
class User < ActiveRecord::Base
include Gravtastic
gravtastic :size => 165, :filetype => :png, :rating => 'R'
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
validates :email, :username, :presence => true, :uniqueness => true
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me
has_many :topics, :dependent => :destroy
has_many :posts, :dependent => :destroy
def admin?
true if self.username == 'admin'
end
end
Here is my registration view:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
And here is my devise registration view:
div class="module" style="padding:15px 25px 0px 25px;">
<div style="float:right; width:100%; padding-left:30px; border-left:1px solid #e2e2e2;">
<%= form_for("user", :as => resource_name, :url => registration_path("user")) do |f| %>
<%= devise_error_messages! %>
<p>
Nome: <br />
<%= f.text_field :name, :style => "font-size:2.0em", :autocomplete => "off" %><br />
</p>
<p>
Usuario: <br />
<%= f.text_field :username, :style => "font-size:2.0em", :autocomplete => "off" %><br />
</p>
<p>
Email: (apenas emails #usp.br sao aceitos) <br />
<%= f.text_field :email, :style => "font-size:2.0em", :autocomplete => "off" %><br />
</p>
<p>
Senha:<br />
<%= f.password_field :password, :style => "font-size:2.0em", :autocomplete => "off" %><br />
</p>
<p>
Confirmar senha:<br />
<%= f.password_field :password_confirmation, :style => "font-size:2.0em", :autocomplete => "off" %><br />
</p>
<p><%= f.submit "Criar" %></p>
<% end %>
</div>
<div class="clear"></div>
</div>
I already tryied rake db:migrate but it still doesnt solve my problem. Sorry, Im still a noob on rails.
EDIT:
Here is my migration file:
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
t.confirmable
# t.encryptable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
# add_index :users, :authentication_token, :unique => true
end
def self.down
drop_table :users
end
end
EDIT 2:
Here is my schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20131121000236) do
create_table "categories", :force => true do |t|
t.string "title"
t.boolean "state", :default => true
t.integer "position", :default => 0
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "forums", :force => true do |t|
t.string "title"
t.text "description"
t.boolean "state", :default => true
t.integer "topics_count", :default => 0
t.integer "posts_count", :default => 0
t.integer "position", :default => 0
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", :force => true do |t|
t.text "body"
t.integer "forum_id"
t.integer "topic_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "topics", :force => true do |t|
t.string "title"
t.integer "hits", :default => 0
t.boolean "sticky", :default => false
t.boolean "locked", :default => false
t.integer "posts_count"
t.integer "forum_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "topics_count", :default => 0
t.integer "posts_count", :default => 0
t.string "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
At least from that migration file, it seems like you are missing name column for users table.
Add the name column for users table.
rails g migration AddNameToUsers
class AddNameToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
end
end
Run rake migrate.
Restart server just to be safe.
FINAL Edit. NEver again!!
In the devise migration file, uncomment
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
rake db:drop db:create db:migrate
restart server

Why does Devise keep asking me that the name can't be blank?

I am new to Rails, and I am working with Devise. The problem I am facing is that my form is not updating the name column of the user when they are signing up. I have the name attribute in the data table, but I can't seem to alter it using a form in devise registration.
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :name %><br />
<%= f.text_field :name, :autofocus => true %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></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.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
end
Scheme.rb
ActiveRecord::Schema.define(version: 20130622203624) do
create_table "posts", force: true do |t|
t.integer "user_id"
t.text "description"
t.integer "comments_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "views", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", limit: 128, default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "views", ["email"], name: "index_views_on_email", unique: true, using: :btree
add_index "views", ["reset_password_token"], name: "index_views_on_reset_password_token", unique: true, using: :btree
end
If you are using rails4 you need strong parameters. If you need to implement extra parameters to devise, you could either create an additional 1-1 relation called profiles for users and store them there (better in the long run of app or if you have more user data) and I personally feel it to be much easier the incorporating your user data in the devise user table.
Alternatively you could do the following if you are using one or two attributes.
You need to permit the name parameter for devise. in your ApplicationController code, do this.
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
end
end
Refer more on this on devise wiki.
Need to make the following changes - and that should fix it
gem 'protected_attributes'
class RegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:name, :user_type,:email, :password, :password_confirmation)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:name,:user_type,:email, :password, :password_confirmation, :current_password)
end
end

Resources