I have a users signup form and once a users signs up he is redirected to users details forms.
user signup form.
<%= form_for(#user, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
<%= f.label :email, class: "required" %>
<%= f.email_field :email, required: true %>
<%= f.label :password, class: "required" %>
<%= f.password_field :password, required: true %>
<%= f.label :password_confirmation, class: "required" %>
<%= f.password_field :password_confirmation, required: true %>
<%= f.submit 'Sign up' %>
<% end %>
user details forms
<%= form_for(#user, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
<%= f.label :user_firstname, "First Name", class: "required" %>
<%= f.text_field :user_firstname, required: true %>
<%= f.label :user_lastname, "Last Name", class: "required" %>
<%= f.text_field :user_lastname, required: true %>
<%= f.submit 'Save' %>
<% end %>
I want to add a feature so that admin can add a users who doesn't have a email and admin just adds a details in users details form and submits and user is created.
What I have done is, I have added a route and added a method in user controller and this is the error I am getting There was an error adding family. Try again . Please help me fix this
Routes
get 'adduser', to:'users#adduser'
post 'adduser', to:'users#create_user'
User controller
def adduser
#user=User.new
end
def create_user
#user=User.new(user_params)
if #user.save
flash[:success] = "User successfully added" redirect_to main_admin_path
else
flash[:danger] = "There was an error adding user. Try again" redirect_to main_admin_path
end
end
<%= form_for(#user, url: adduser_path, :html => { multipart: true, class: "form-horizontal", role: "form"}) do |f| %>
<%= f.label :user_firstname, "First Name", class: "required" %>
<%= f.text_field :user_firstname, required: true %>
<%= f.label :user_lastname, "Last Name", class: "required" %>
<%= f.text_field :user_lastname, required: true %>
<%= f.submit 'Save' %>
<% end %>
Validations
attr_accessor :remember_token, :activation_token, :reset_token
before_create :confirmation_token
has_secure_password
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: { with: VALID_EMAIL_REGEX }
validates_attachment :document
validates_confirmation_of :password
At first, you need to create a migration and add a new column to the users table. I think it is better to keep info about admin creating in the db (if you don't want to store it, just add :created_by_admin to attr_accessor list instead of this step).
add_column :users, :created_by_admin, :boolean, default: false, null: false
Add this new field to the form ('added by admin'):
<%= f.hidden_field :created_by_admin, value: true %>
Add condition to email validation:
validates :email, presence: true, uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }, unless: :created_by_admin?
Use an attr_accessor in your model, if you don't want any changes to your existing schema.
models/user.rb
class User
attr_accessor :with_email_optional
# Run these validations only if `self.with_email_optional` is not set OR set to `false`
validates :email,
presence: true,
uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX },
unless: :with_email_optional
end
And in your users controller for admin (i don't know what that is called in your application):
def new
#user = User.new
#user.with_email_optional = true
end
private
# To be used in `create` action
def user_params
params.require(:user).permit(..., :with_email_optional)
end
Form
<%= form_for(#user) do |f|
<%= f.hidden_field :with_email_optional, value: f.object.with_email_optional %>
<% end %>
Related
I install devise gem and wanted to add some columns on registration page.
app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up){|u| u.permit(:company_id, :name, :email, :profile, :prefecture_id, :address, :password, :password_confirmation)}
end
app/views/devise/registrations/new.html.erb
<%= simple_form_for(resource, as: resource_name, url:registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.label :campany_id %><br>
<%= f.collection_select :campany_id, Campany.all, :id, :name, include_blank: true %>
<%= f.input :name, required: true, autofocus: true %>
<%= f.inneput :email, paceholder:"メールアドレス", required: true, autofocus: true %>
<%= f.input :profile, required: true, autofocus: true %>
*<%= f.label :prefecture_id %><br>
<%= f.collection_select :prefecture_id, JpPrefecture::Prefecture.all, :code, :name %>
<%= f.input :address, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "新規登録" %>
</div>
<% end %>
config/routes.rb
devise_for :users, controllers: {
registrations: 'users/registrations'
}
When I filled out the form and submitted. It show " ArgumentError:wrong number of arguments (given 0, expected 1)" this error!
Is there some wrong?
Thank you!
In new.html.erb, for input email your input spelling should be f.input, also you have given campany_id but in controller you have used company_id.
And in controller:
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up){|u| u.permit(:company_id, :name, :email, :profile, :prefecture_id, :address)}
end
You only need to permit parameters which have been added by you. You do not need to permit password or confirm_password.
Also all the parameters permitted or which are there in form need to be in your table.
Hope this helps.
on click on submit it should go to update_settings method of dashboard_controller. on click i gets this error.How to solve this
Any help is appreciatable
undefined methodmodel_name' for 1:Fixnum`
`
i have my routes like this
namespace :generic_users do
root to: 'dashboard#show'
get 'settings', to: 'dashboard#settings'
patch "settings" => "dashboard#update_settings", :as => "update_settings"
end
my dashboardcontroller is like this
class GenericUsers::DashboardController<ApplicationController
before_filter :authenticate_user!
def settings
#title='Generic Users Profile Management Page'
#generic_user = current_user.specific.id
end
def update_settings
#generic_user = GenericUsers.find(current_user.specific.id)
if #generic_user.update_attributes(sign_up_params)
redirect_to generic_users_root_path, flash: {notice: "Your details have been updated"}
else
render "new"
end
end
private
def sign_up_
end
private
def sign_up_params
params.require(:generic_user).permit(:username,:mobile,:firstname,:lastname, :email,:image,:referred_by,:address)
end
end
in views/dashboard folder, view file called,
settings.html.erb
<%= simple_form_for(#generic_user, :url => generic_users_update_settings_path) do |f| %>
<div class="form-inputs">
<%= f.input :username, required: true, autofocus: true %>
<%= f.input :firstname, required: true, autofocus: true %>
<%= f.input :lastname, required: true, autofocus: true %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :mobile, required: true, autofocus: true %>
<%= f.input :image, required: true, autofocus: true %>
<%= f.input :referred_by, required: true, autofocus: true %>
<%= f.label :address, "Address" %><br>
<%= f.text_area :address, required: true, autofocus: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
also have a model generic_user.rb
Please give me a solution to this error.
i have no idea
undefined method `model_name' for 1:Fixnum
You need to change the #generic_user in settings method as below
def settings
#title='Generic Users Profile Management Page'
#generic_user = GenericUsers.find(current_user.specific.id)
end
The first argument in the simple_form_for should be a record.
I have this simple_form in my app but it doesn't display any validation error messages:
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<%= f.input :first_name, label: "Prénom" %>
<%= f.input :last_name, label: "Nom" %>
<%= f.input :email, label:"email" %>
<%= f.input :telephone, label: "telephone"%>
<p><%= f.label :birthdate, 'Date de naissance' %></p>
<%= f.date_select :birthdate, {:include_blank => true, :default => nil, :use_month_names => ['Janv.','Fevr.', 'Mars', 'Avr.', 'Mai', 'Juin', 'Juil.', 'Août','Sept.', 'Oct.', 'Nov.', 'Déc.'], :order => [:day, :month, :year], :start_year => 1910, :end_year => 1995} %>
<%= f.input :genre, label: "Sexe" %>
<%= f.input :ranking, label:"Classement" %>
<%= f.input :licence_number, label: "numéro de licence"%>
<p>
<%= f.label :Photo%>
<%= f.file_field :picture %>
</p>
<p>
<%= f.label :licence %>
<%= f.file_field :licencepicture %>
</p>
<p>
<%= f.label :certificat %>
<%= f.file_field :certifmedpicture %>
</p>
<div id="validation"><%= f.submit %></div>
<% end %>
Here are my validations in my user model
validates :first_name, presence: { strict: true }, on: :update
validates :last_name, presence: { strict: true }, on: :update
and you can have a look at my simple_form.en.yml:
en:
simple_form:
"yes": 'Yes'
"no": 'No'
required:
text: 'required'
mark: '*'
# You can uncomment the line below if you need to overwrite the whole required html.
# When using html, text and mark won't be used.
# html: '<abbr title="required">*</abbr>'
error_notification:
default_message: "Certains champs posent problèmes:"
My update method:
def update
#user.update(user_params)
#user.create_mangopay_natural_user_and_wallet
redirect_to user_path(current_user)
end
I don't get why the error notifications don't display as I have the proper f.error_notification in my view
As revealed in your comments, your controller doesn't check for a successful save and just blindly redirects to the user page. I didn't see any native functionality from simple_form that does this for you. You need to check for this successful save and re-render the edit form on failure so it can show those errors in f.error_notification:
def update
if #user.update(user_params)
#user.create_mangopay_natural_user_and_wallet
redirect_to user_path(current_user)
else
render 'edit'
end
end
I have implemented a user authentication system in rails using the gem 'bcrypt';I would like to insert a current password field to the edit form to make the changes to the password.
How can do this?
class User < ActiveRecord::Base
before_save { self.email = email.downcase}
before_create :create_remember_token
#Associations
has_one :profile
has_many :posts
#validations
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: {minimum: 6}
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Thank you
In your view file:
<%= f.label :current_password %>
<%= f.password_field :current_password %>
Also make sure you permit the current_password parameter in your controller.
I assumed current_password attr is already defined by has_secured_password.
So I'm trying to get my new/create actions working with form_for for my resource 'project' but the create form in 'new.html.erb' seems to think that the parameter 'version' is blank when I submit it, even though I am setting it equal to 1.
Projects Controller
def new
#project = current_user.projects.build if user_signed_in?
#project.version = 1
#project.unique_id = rand(1000000)
while(Project.find_by_unique_id(#project.unique_id) != nil)
#project.unique_id = rand(1000000)
end
end
def create
#project = current_user.projects.build(project_params)
if #project.save
flash[:success] = "Project created!"
redirect_to user_url(current_user.username)
else
render 'new'
end
end
private
def project_params
params.require(:project).permit(:description, :name, :media, :content_type, :file_size, :unique_id, :verison)
end
end
New.html.erb
<%= form_for(#project, html: {multipart: true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div>
<%= f.label :name %>
<%= f.text_field :name, maxlength: 50, class: 'form-control' %>
</div>
<div>
<%= f.label :description %>
<%= f.text_area :description, placeholder: "What is it all about?", class: 'form-control' %>
</div>
<%= f.file_field :media, accept: 'image/jpeg,image/gif,image/png,audio/mpeg,audio/vnd.wave' %>
<%= f.hidden_field :version %>
<%= f.hidden_field :unique_id %>
<%= f.submit "Create Project", class: "btn btn-primary" %>
<% end %>
Project Model
class Project < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
validates :user_id, presence: true
validates :name, presence: true, length: { maximum: 50 }
validates :unique_id, presence: true
validates :version, presence: true
end
Thanks so much in advance!
I would set it equal to one in the form as a hidden field like this:
<div>
<%= f.hidden_field :version, value: 1 class: 'form-control' %>
</div>
I would also get rid of #project.version = 1 in your project new action.
Figured it out thanks to JTG. There appeared to be a typo in my strong params which was causing 'version' to be filtered out and thus not passed through.
Thanks everyone!