param not found: first_name | ActionController::ParameterMissing in LegaciesController#create - ruby-on-rails

I am getting the error: param not found: first_name | ActionController::ParameterMissing in LegaciesController#create
On
private
def link_params
params.require(:first_name).permit(:id, :last_name, :legacy_donations, :address, :city, :state, :zip_code, :gender, :date_of_birth, :Marital_status, :Spouses_name, :comments)
end
end
This is my controller
class LegaciesController < ApplicationController
def index
end
def new
#legacy = Legacy.new
end
def create
#legacy = Legacy.new(link_params)
if #legacy.save
redirect_to #legacy
else
render action: 'new'
end
end
def show
end
def destroy
end
def edit
end
def update
end
private
def link_params
params.require(:first_name).permit(:id, :last_name, :legacy_donations, :address, :city, :state, :zip_code, :gender, :date_of_birth, :Marital_status, :Spouses_name, :comments)
end
end
And this is my legacies/new view:
%= form_for #legacy, html:{ role: "form", class: "new-legacy"} do |f| %>
<% if #legacy.errors.any? %>
<div id="error_explanation">
<h1><%= pluralize(#legacy.errors.count, "error") %> prohibited this from being saved:</h1>
<ul>
<% #legacy.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :First_Name %><br />
<%= f.text_field :first_name, class:"form-control", placeholder:"First Name" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Last_Name %><br />
<%= f.text_field :last_name, class:"form-control", placeholder:"Last Name" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Last_Name %><br />
<%= f.text_field :last_name, class:"form-control", placeholder:"Last Name" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Last_Name %><br />
<%= f.text_field :last_name, class:"form-control", placeholder:"Last Name" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Address %><br />
<%= f.text_field :address, class:"form-control", placeholder:"Adress" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :City %><br />
<%= f.text_field :city, class:"form-control", placeholder:"City" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :State %><br />
<%= f.text_field :state, class:"form-control", placeholder:"State" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Zip_Code %><br />
<%= f.text_field :zip_code, class:"form-control", placeholder:"Zip Code" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Gender %><br />
<%= f.text_field :gender, class:"form-control", placeholder:"Gender" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :DOB %><br />
<%= f.date_field :date_of_birth, class:"form-control", placeholder:"DOB" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Marital_Status %><br />
<%= f.text_field :gender, class:"form-control", placeholder:"Marital Status" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Spouses_Name %><br />
<%= f.text_field :gender, class:"form-control", placeholder:"Spouses Name" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Legacy_Donations %><br />
<%= f.text_field :legacy_donations, class:"form-control", placeholder:"Legacy Donations" %>
</div>
<div class="form-group">
<div class="field">
<%= f.label :Comments %><br />
<%= f.text_field :comments, class:"form-control", placeholder:"Comments" %>
</div>
<div class="actions">
<%= f.submit class:"btn btn-default new-eurekamoment-button" %>
</div>
<% end %>
</div>
Whats the deal am I overlooking something?

It should be
def link_params
params.require(:legacy).permit(:id, :first_name,:last_name, :legacy_donations, :address, :city, :state, :zip_code, :gender, :date_of_birth, :Marital_status, :Spouses_name, :comments)
end

Related

Problem with nested attributes for Devise registration page

I need to add Address nested attributes for User.
But only user_id hits the addresses table when I submit, and not country city street and zip_code
user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates_presence_of :name, :age, :email
has_one :address, dependent: :destroy
accepts_nested_attributes_for :address
address.rb
attr_accessor :country, :city, :zip_code, :street
belongs_to :user
appication_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
...
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :age,
address_attributes: [:country, :city, :zip_code, :street]])
end
The form
<% resource.build_address %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :name %><br />
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :age %><br />
<%= f.number_field :age, class: 'form-control' %>
</div>
<h4>Address</h4>
<%= f.fields_for :address do |address| %>
<div class="form-group">
<%= address.label :country %>
<%= address.text_field :country, class: 'form-control' %>
</div>
<div class="form-group">
<%= address.label :city %>
<%= address.text_field :city, class: 'form-control'%>
</div>
<div class="form-group">
<%= address.label :street %>
<%= address.text_field :street, class: 'form-control'%>
</div>
<div class="form-group">
<%= address.label :zip_code %>
<%= address.text_field :zip_code, class: 'form-control' %>
</div>
<% end %>
<div class="form-group">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>
<% end %>

Rails 5: accepts nested attributes for

I'm trying to implement accepts_nested_attributes_for on my app. When I add accepts_nested_attributes_for :account to the user.rb the nested fields disappear. When I delete accepts_nested_attributes_for :account the fields appear but no data is being saved when I click on submit.
Any ideas why this is happening?
I have two models with appropriate associations added:
user.rb
has_one :account
has_many :items
accepts_nested_attributes_for :account
account.rb
belongs_to :user
app/controllers/users/registrations.controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
def new
build_resource({})
resource.build_account
respond_with self.resource
session[:registration_params] = request.query_parameters
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u|
u.permit(:email, :password, :password_confirmation, :remember_me,
:account_attributes => [:first_name, :last_name, :buisness_name,
:buisness_description, :web_site, :phone_number,
:street, :city, :state, :zip_code, :country])
}
end
new.html.erb
<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, autocomplete: "off" %>
</div>
<%= f.fields_for :account do |form| %>
<div class="field">
<%= form.label :first_name %>
<%= form.text_field :first_name, id: :account_first_name %>
</div>
<div class="field">
<%= form.label :last_name %>
<%= form.text_field :last_name, id: :account_last_name %>
</div>
<div class="field">
<%= form.label :buisness_name %>
<%= form.text_field :buisness_name, id: :account_buisness_name %>
</div>
<div class="field">
<%= form.label :buisness_description %>
<%= form.text_field :buisness_description, id: :account_buisness_description %>
</div>
<div class="field">
<%= form.label :web_site %>
<%= form.text_field :web_site, id: :account_web_site %>
</div>
<div class="field">
<%= form.label :phone_number %>
<%= form.text_field :phone_number, id: :account_phone_number %>
</div>
<div class="field">
<%= form.label :street %>
<%= form.text_field :street, id: :account_street %>
</div>
<div class="field">
<%= form.label :city %>
<%= form.text_field :city, id: :account_city %>
</div>
<div class="field">
<%= form.label :state %>
<%= form.text_field :state, id: :account_state %>
</div>
<div class="field">
<%= form.label :zip_code %>
<%= form.text_field :zip_code, id: :account_zip_code %>
</div>
<div class="field">
<%= form.label :country %>
<%= form.text_field :country, id: :account_country %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
I had to add the controller to routes.rb devise_for :users
devise_for :users, controllers: {registrations: 'users/registrations'}
You can use cocoon gem for nested attributes. It will make handling nested attributes lot more easier

How to Force the User to Complete the Whole Form?

I am creating a Sing Up form. I need the user to complete every field of the form, but the rails only blocks the creation when there is no password. What I have to do to force the user to complete the whole form before submiting?
Here's my form code:
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label :fav_team, "Favorite Team" %>
<%= collection_select(:user, :fav_team, League.order(:name), :id, :name, {:include_blank => "Select a League"}, { :id => "leagues_select"}) %>
<%= grouped_collection_select(:user, :fav_team, League.order(:name), :teams, :name, :id, :name, {:include_blank => true}, {}) %>
</div>
<div class="field">
<%= f.label :net_worth, "Net Worth (USD)" %>
<%= f.text_field :net_worth, :readonly => true, :value => "100" %>
</div>
<div class="field">
<%= f.label :country %>
<%= f.select :country, options_for_Countrys, :include_blank => true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You have to write validations in your models to make sure the data is valid and present.
In your case you have to do something like this
class User < ApplicationRecord
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
end
You can check here for more information

adding a text area field to devise edit form

I have an attribute called discount_info. I would like to create another field in the devise edit form, so user can edit this information. I would like to have the field be a text_area. I'm not sure how to do this in rails.
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.email_field :address, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<%= f.text_area :discount_info, :class => "", :placeholder => "if you want a placeholder" %>
Just add the attribute in the form and permit it in the configure_permitted_parameters method.
<div class="field">
<%= f.label :discount_info %><br />
<%= f.text_area :discount_info %>
</div>
And in the controller
def configure_permitted_parameters
#other code
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :discount_info) }
end

Devise multiple step form for registration

I want the user to be able to register in two steps as I have many fields. Ideally first step would be to accept email and password. As user enter it, they can proceed to fill the next step.
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %></br>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :gender %>
<span class="option">Male</span><%= f.radio_button :gender, "m" %>
<span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
</div>
<div class="field">
<%= f.label :mobile_no %></br>
<%= f.telephone_field :mobile_no %>
</div>
<div class="field">
<%= f.label :website %>
<%= f.url_field :website %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :skills %>
<%= f.text_field :skills %>
</div>
<div class="field">
<%= f.label :passion %>
<%= f.text_field :passion %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<% end %>
I tried the approach where user enters email and password and after that they are redirected to edit page where they can update other fields.
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
end
routes.rb
devise_for :users, :pro, :amateur, controllers: { registrations: "registrations" }
However, the redirect doesn't seem to work. I also tried after_inactive_sign_up_path_for as I am using confirmable, still not working.
I can't seem to figure out why this could be also I would like to know if there are any other approach without using any gem?
You could use javascript if you're just interested in user interface. Or if it's important that you're saving all the info as you're going, you can have the form split into partials and override the devise registration controller to render those separate partials.
You will also need to add steps that link with the partials in your user model. I think if you used a gem, this would probably be main part that it will do for you.
this is a great railscast on how to have a multistep form without any gems. the only difference is that you'll need to override the create method in devise

Resources