I am trying to create a custom admin form for creating new users.
Here is the form:
admins/newwebsite.html.erb
<%= form_for(#branch) do |f| %>
<div><%= f.label :branch %><br />
<%= f.text_field :branch, autofocus: true %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
controller: controllers/admins_controller.rb
def newwebsite
#branch = Branch.new
end
def newwebsiteparams
params.require(:branch).permit(:branch, :email, :password, :password_confirmation)
end
def create
#branch = Branch.create(newwebsiteparams)
end
routes:
get 'admins/dashboard'
get 'admins/newwebsite'
post 'admins/create' => 'admins#create'
error:
undefined method `branches_path' for #<#<Class:0x57414d8>:0x57409f8>
Any help would certainly be appreciated.
Related
I have two types of registration on on my website.
User
Host
There are two models related to Registration
users(id, name, is_host, ...)
hosts(company_name, user_id, status, ...)
Every Host is a User by default on the application. When the User signups on the website he has to enter the following fields
Name
Email
Password
Phone Number
When a Host Signups he has to enter the following
Name
Email
Password
Phone Number
Company Name
On submitting the form as a host it should save the data in the User model and also set the is_host flag to 1 which other wise is 0 and then store the company_name in the hosts model.
What i have done?
I have installed devise and generated the migration tables and generated the scoped views.
What i am trying to achieve?
Add custom fields in the signup form, How to set the strong parameter for the User sign up form?
how to have a separate signup form for the same model and act as a registration for HOSTS
views/users/registrations/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 :first_name %><br />
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= 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 %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
Server Error:
Unpermitted parameters: first_name, last_name
Please look this example....
we create the migration – nothing special here
class AddFieldsToUsers < ActiveRecord::Migration
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
end
end
app/views/devise/registrations/new.html.slim
h2 Sign up
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= devise_error_messages!
div
= f.label :first_name
br
= f.text_field :first_name, autofocus: true
div
= f.label :last_name
br
= f.text_field :last_name
div
= f.label :email
br
= f.email_field :email
div
= f.label :password
br
= f.password_field :password
div
= f.label :password_confirmation
br
= f.password_field :password_confirmation
div
= f.submit 'Sign up'
= render 'devise/shared/links'
Customizing the RegistrationsController
def sign_up_params
devise_parameter_sanitizer.sanitize(:sign_up)
end
def account_update_params
devise_parameter_sanitizer.sanitize(:account_update)
end
registrations controller
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
end
config/routes.rb
devise_for :users, :controllers => { registrations: 'registrations' }
I'm using devise and I want to add the fields first_name and last_name to the sign_up form.
After creating the respective columns, I tried to sign_up with the new form, but the values of the new fields in DB were nil.
After that I created this controller:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
end
But I still get nil in the columns first_name and last_name.
Am I missing something?
The form:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :last_name %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.label :password %> <% if #validatable %><i>(<%= #minimum_password_length %> characters minimum)</i><% end %><br />
<%= f.password_field :password, autocomplete: "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
1 option.
I think adding the following to application_controller.rb would eliminate the issue (instead of overridding the devise's controller):
private
def configure_devise_params
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
end
2 option.
Check passing the controllers: { registrations: 'devise/registrations' } block do devise_for :users line in routes.rb
did you add devise_for :users, :controllers => { registrations: 'devise/registrations' } in your routes file ? If not then add it in your routes file. Other things are looks Okay
rails g devise:views command generated that view
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
<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.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>
My question is how does it work " form_for(resource, : as=>resource_name, ..."
resource is simple a record (or object) - with respect to Devise, it's usually something called User or similar. The rest of the parameters for form_for are options, detailed in the form_for docs.
I notice that both the :email and :username are calling for the email_field, which is a mistake.
I've added 2 new fields on my devise users: :first_name and :last_name
So in the registration page I've added these new fields to be filled by user.
<div>
<%= f.label :first_name %> <br>
<%= f.text_field :first_name, autofocus => true %>
</div>
<div>
<%= f.label :last_name %> <br>
<%= f.text_field :last_name %>
</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>
Initially the autofocus was set on :email field, but now I changed to :first_name field.
The problem is when I access this page i get this error:
undefined local variable or method `autofocus' for #<#<Class:0x361ef18>:0x449d630>
What forgot I to do in my rails app?
You forgot the : before the autofocus symbol, change it to:
<%= f.text_field :first_name, :autofocus => true %>
it should be :autofocus and not autofocus
<%= f.text_field :first_name, :autofocus => true %>
The colon : is missing at the start, should be a symbol, else its treated as if local variable/assign.
When I go to the edit_user_registration_path provided by Devise I have fields such as email and username pre-filled.
I want to create custom user profile page with the similar form for edit user information containing additional fields of dependent model. And I want fields be pre-filled.
How should I do it properly?
The default form provided by Devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
Use nested attributes
In your devise user model, if you have other dependent models, you can add a relationship between those models, like has_many and belongs_to. The do this:
Devise user model:
accepts_nested_attributes_for :name_of_other_model
Then in your form you can use fields_for. fields_for docs
Here's also a great railscast of this here