Editing user details creates new user. Devise, Rails 4 - ruby-on-rails

I have weird problem, previous created form instead of updating user details such as e-mail or name creates new user with given details.
My form:
this code example I took from views/devise/registrations/edit
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { "data-parsley-validate" => true, :id=>"user-edit"},remote: true, format: :json) do |f| %>
<div class="form-group">
<%= f.text_field :name,:class=>"user-input form-control", :id=>"user-name" ,:placeholder=> "Lietotājvārds*",:"data-parsley-group"=>"f1" %>
</div>
<div class="form-group">
<%= f.email_field :email ,:class=>"user-input form-control", :id=>"password",:placeholder=> "E-pasts *",:"data-parsley-group"=>"f2" %>
</div>
<div class="form-group">
<%= f.password_field :current_password, :autocomplete => "off" ,:class=>"user-input form-control", :id=>"password",:placeholder=> "Vecā parole* ",:"data-parsley-group"=>"f3" %>
</div>
<div class="form-group">
<%= f.password_field :password , :class=>"user-input form-control", :id=>"password",:placeholder=> "Jaunā parole* vismaz 8 simboli ", :"data-parsley-group"=>"f3" %>
</div>
<div class="form-group">
<%= f.password_field :password_confirmation , :class=>"user-input form-control", :id=>"password",:placeholder=> "Atkārtot paroli * vismaz 8 simboli ", :"data-parsley-group"=>"f3" %>
</div>
<button type="submit" class="blue-button btn btn-default">Apstiprināt</button>
<%end%>
Routes file:
devise_for :users, :controllers => {:registrations=> "registrations"}
Registration controller:
class RegistrationsController < Devise::RegistrationsController
clear_respond_to
respond_to :json
def sign_up_params
params.require(:user).permit( :email, :password, :password_confirmation,:name, :not_a_robot,:current_password,:bypass_humanizer)
end
def account_update_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :current_password, :not_a_robot, :bypass_humanizer)
end
private :sign_up_params
private :account_update_params
protected
def update_resource(resource, params)
resource.update_without_password(params)
end
end
In applicaion helper:
module ApplicationHelper
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
Log file:
Started POST "/ru/users" for 85.255.65.15 at 2015-09-28 19:32:25 +0300
Processing by RegistrationsController#create as JS
Parameters: {"utf8"=>"✓", "user"=>{"name"=>"ooppapa", "email"=>"test11#!!!", "current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "bypass_humanizer"=>"true", "not_a_robot"=>"1"}, "locale"=>"ru"}
[1m[36mBanlist Load (2.0ms)[0m [1mSELECT `banlists`.* FROM `banlists` WHERE (ip_adress = '85.255.65.15')[0m
[1m[35mCountry Load (1.5ms)[0m SELECT `countries`.* FROM `countries` WHERE `countries`.`id` = 1 LIMIT 1
[1m[36mRegion Load (1.3ms)[0m [1mSELECT `regions`.* FROM `regions` WHERE `regions`.`country_id` = 1[0m
[1m[35m (0.4ms)[0m BEGIN
[1m[36mUser Exists (44.1ms)[0m [1mSELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test11#!!!' LIMIT 1[0m
[1m[35mUser Load (24.3ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`confirmation_token` = '5c5e282bd4c139c7764506b785d54119ceee5499426b555c3650cfc7190ee947' ORDER BY `users`.`id` ASC LIMIT 1
[1m[36mSQL (2.1ms)[0m [1mINSERT INTO `users` (`confirmation_sent_at`, `confirmation_token`, `created_at`, `email`, `encrypted_password`, `name`, `updated_at`) VALUES ('2015-09-28 19:32:29', '5c5e282bd4c139c7764506b785d54119ceee5499426b555c3650cfc7190ee947', '2015-09-28 19:32:28', 'test11#individualki.eu', '$2a$10$mEHajmY0H1NueGrrap7NNu0LuViDEJ.imAS4jhdj1KIyPRIyej/NC', 'ooppapa', '2015-09-28 19:32:28')[0m
Rendered devise/mailer/confirmation_instructions.html.erb (29.2ms)
Devise::Mailer#confirmation_instructions: processed outbound mail in 1726.8ms
Sent mail to test11#!!!.eu (776.1ms)
Date: Mon, 28 Sep 2015 19:32:31 +0300
From: support#!!!!.eu
Reply-To: support#!!!!.eu
To: test11#!!!!.eu
Message-ID: <56096b9f98eb5_25e4640a718550c6#!!!!!>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome !!!</p>
<p>You can confirm your account email through the link below:</p>
My routes:
rake routes
new_user_session GET (/:locale)/users/sign_in(.:format) devise/sessions#new {:locale=>/lv|ee|ru/}
user_session POST (/:locale)/users/sign_in(.:format) devise/sessions#create {:locale=>/lv|ee|ru/}
destroy_user_session DELETE (/:locale)/users/sign_out(.:format) devise/sessions#destroy {:locale=>/lv|ee|ru/}
user_password POST (/:locale)/users/password(.:format) devise/passwords#create {:locale=>/lv|ee|ru/}
new_user_password GET (/:locale)/users/password/new(.:format) devise/passwords#new {:locale=>/lv|ee|ru/}
edit_user_password GET (/:locale)/users/password/edit(.:format) devise/passwords#edit {:locale=>/lv|ee|ru/}
PATCH (/:locale)/users/password(.:format) devise/passwords#update {:locale=>/lv|ee|ru/}
PUT (/:locale)/users/password(.:format) devise/passwords#update {:locale=>/lv|ee|ru/}
cancel_user_registration GET (/:locale)/users/cancel(.:format) registrations#cancel {:locale=>/lv|ee|ru/}
user_registration POST (/:locale)/users(.:format) registrations#create {:locale=>/lv|ee|ru/}
new_user_registration GET (/:locale)/users/sign_up(.:format) registrations#new {:locale=>/lv|ee|ru/}
edit_user_registration GET (/:locale)/users/edit(.:format) registrations#edit {:locale=>/lv|ee|ru/}
PATCH (/:locale)/users(.:format) registrations#update {:locale=>/lv|ee|ru/}
PUT (/:locale)/users(.:format) registrations#update {:locale=>/lv|ee|ru/}
DELETE (/:locale)/users(.:format) registrations#destroy {:locale=>/lv|ee|ru/}
user_confirmation POST (/:locale)/users/confirmation(.:format) devise/confirmations#create {:locale=>/lv|ee|ru/}
new_user_confirmation GET (/:locale)/users/confirmation/new(.:format) devise/confirmations#new {:locale=>/lv|ee|ru/}
GET (/:locale)/users/confirmation(.:format) devise/confirmations#show {:locale=>/lv|ee|ru/}
update_password_user PATCH (/:locale)/user/update_password(.:format) users#update_password {:locale=>/lv|ee|ru/}
edit_user GET (/:locale)/user/edit(.:format) users#edit {:locale=>/lv|ee|ru/}
sms_receive GET (/:locale)/sms/receive(.:format) sms#receive {:locale=>/lv|ee|ru/}
root GET /(:locale)(.:format) girls#index {:locale=>/lv|ee|ru/}
I am confused. Is it registration_path(resource_name) that creates this error?
Any advise could be helpful.!
thanks in advance.

The issue is in the form as it is pointing to the registration_path(resource_name) path.
The example you are using is taken from How To: Allow users to edit their account without providing a password and it says:
and provide an edit and update actions, as you would do for any other
resource in your application.
Which means you need to create a route to the edit method and use it as you wish.
The example above is for updating the user without the password, but what you are doing is updating the password.
For the solutions, read How To: Allow users to edit their password
An example solution - #3:
UsersController
class UsersController < ApplicationController
before_filter :authenticate_user!
def edit
#user = current_user
end
def update_password
#user = User.find(current_user.id)
if #user.update(user_params)
# Sign in the user by passing validation in case their password changed
sign_in #user, :bypass => true
redirect_to root_path
else
render "edit"
end
end
private
def user_params
# NOTE: Using `strong_parameters` gem
params.require(:user).permit(:password, :password_confirmation)
end
end
If you are using several scopes, specify the one you are signing in:
sign_in :user, #user, bypass: true
The route should be the following:
resource :user, only: [:edit] do
collection do
patch 'update_password'
end
end
View
<%= form_for(#user, :url => { :action => "update_password" } ) do |f| %>
<div class="field">
<%= f.label :password, "Password" %><br />
<%= f.password_field :password, :autocomplete => "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="action_container">
<%= f.submit %>
</div>
<% end %>
To use "confirm_password" field to force user to enter old password
before updating with the new one: Change #user.update(user_params) to
#user.update_with_password(user_params) in the controller along with
adding :current_password to the permitted parameters, then and add the
following to the view code:
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %>
</div>
Remember, Devise models are like any model in your application. If you
want to provide custom behavior, just implement new actions and new
controllers. Don't try to bend Devise.

I saw your question and maybe the problem is on form call.
Try insert html: { method: :put }. With simple_form I use this way:
= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
How you omitted the method of send the form, it's sending as post and creating a new record, I presume.

Related

ruby on rails devise edit not working when i use modal

I am using devise gem and Semantic UI modal.
I do sign-in and sign-up in homepage.html.erb.
I changed
devise/session/new.html.erb to devise/sesseion/_new.html.erb
so i can use <%= render devise/session/new %>
Sign-up and sign-in are working well when I use modal but edit is not working when using modal.
What is the problem in my code?
homepage.html.erb
<div class="ui longer modal">
<div>
<%= render "devise/registrations/edit" %>
</div>
</div>
devise/registrations/_edit.html.erb
<%= resource_name.to_s.humanize %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:remote => true, :class => 'ui form'}) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :tel %><br />
<%= f.number_field :tel, autofocus: true, placeholder: current_user.tel, autocomplete: "tel" %>
</div>
<div class="field">
<%= f.label :nickname %><br />
<%= f.text_field :nickname, autofocus: true, placeholder: current_user.nickname, autocomplete: "nickname" %>
</div>
<div class="field">
<%= f.label :profile_img %><br />
<%= f.text_field :profile_img, autofocus: true, autocomplete: "profile_img" %>
</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" %>
<% if #minimum_password_length %>
<br />
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
</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 "edit" %>
</div>
<% end %>
<%= button_to "membership withdrawal", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>
route.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { :passwords => "users/passwords", :confirmations => "users/confirmations", registrations: 'users/registrations', sessions: 'users/sessions'}
devise_scope :user do
get 'registrations' => 'devise/registrations#create'
get 'users/edit' => 'devise/registrations#edit'
delete 'users/sign_out' => 'devise/sesssions#destroy'
end
application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
additional_params = [:name, :tel, :nickname, :profile_img]
devise_parameter_sanitizer.permit(:sign_up, keys: additional_params)
devise_parameter_sanitizer.permit(:tel, :nickname, :password, :password_confirmation, :current_password)
end
Routes
Helper HTTP Verb Path Controller#Action
new_user_session_path GET /users/sign_in(.:format) users/sessions#new
user_session_path POST /users/sign_in(.:format) users/sessions#create
destroy_user_session_path GET /users/sign_out(.:format) users/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) users/registrations#cancel
user_registration_path POST /users(.:format) users/registrations#create
new_user_registration_path GET /users/sign_up(.:format) users/registrations#new
edit_user_registration_path GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
registrations_path GET /registrations(.:format) devise/registrations#create
GET /users/edit/:id(.:format) devise/registrations#edit
users_path PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) users/registrations#destroy
users_sign_out_path DELETE /users/sign_out(.:format) devise/sesssions#destroy
root_path GET / fjob#homepage
fjob_homepage_path GET /fjob/homepage(.:format) fjob#homepage
fjob_create_path GET /fjob/create(.:format) fjob#create
review_create_review_path GET /review/create_review(.:format) review#create_review
users_sign_up_path GET /users/sign_up(.:format) users#sign_up
fjob_default_fjob_path GET /fjob/default_fjob(.:format) fjob#default_fjob
GET /users/sign_out(.:format) users#sign_out
users_edit_path GET /users/edit(.:format) users#edit
application_helper.rb
module ApplicationHelper
def resource_name
:user
end
def resource
#resource ||= User.new
end
def resource_class
User
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
Logs
started POST "/users" for 222.98.34.68 at 2018-08-23 08:19:01 +0000
Cannot render console from 222.98.34.68! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"tel"=>"123456", "nickname"=>"123123", "profile_img"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"회원 정보 수정"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]]
Redirected to https://find-kim-esdx245.c9users.io/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 3ms (ActiveRecord: 0.2ms)
Started GET "/" for 222.98.34.68 at 2018-08-23 08:19:01 +0000
Cannot render console from 222.98.34.68! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by FjobController#homepage as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]]
Fjob Load (0.1ms) SELECT "fjobs".* FROM "fjobs" WHERE "fjobs"."id" = ? LIMIT 1 [["id", 5]]
Rendered devise/sessions/_new.html.erb (4.9ms)
Rendered devise/registrations/_new.html.erb (4.0ms)
Rendered devise/registrations/_edit.html.erb (7.2ms)
Fjob Load (0.3ms) SELECT "fjobs".* FROM "fjobs"
Fjob Load (0.2ms) SELECT "fjobs".* FROM "fjobs" WHERE "fjobs"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Review Load (0.1ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "fjobs".* FROM "fjobs" WHERE "fjobs"."id" = ? LIMIT 1 [["id", 5]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 5]]
Review Load (0.1ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = ? LIMIT 1 [["id", 5]]
Fjob Load (0.1ms) SELECT "fjobs".* FROM "fjobs" WHERE "fjobs"."id" = ? LIMIT 1 [["id", 3]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
Review Load (0.1ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."id" = ? LIMIT 1 [["id", 3]]
Rendered fjob/homepage.erb within layouts/application (25.1ms)
Completed 200 OK in 29ms (Views: 26.5ms | ActiveRecord: 1.4ms)

Rails 5 Edit Form can't find path with nested model - routing error

I have a controller that handles the new and edit actions for a form with a model that accepts nested attributes form another. The 'new' action works just fine. However, when I go to the edit form and submit it, it says:
Routing Error
No route matches [PATCH] "/admins/employees"
Also, when I am on the edit page it won't show all the current information there. Only the 'email' shows what is currently in the DB. Normally, the edit page shows what is currently in the DB related to those attributes, but this form is just blank, with the exception of the email. Unfortunately, googling this particular issue didn't come up with anything relevant or helpful. I think there was something close that was with Rails 3 or something but wasn't right for my problem. I am using Rails 5.1.
My code is as follows:
Employee Controller
class Admins::EmployeesController < UserActionsController
def index
#employees = User.where(:company => #current_company)
end
def edit
#employee = User.find(params[:id])
end
def update
#employee = User.find(params[:id])
#employee.assign_attributes(employee_params)
if #employee.save
flash[:notice] = "Employee was updated."
redirect_to root_path
else
flash.now[:alert] = "There was an error saving the information. Please try again."
render :edit
end
end
def show
#employee = User.find(params[:id])
end
def new
#employee = User.new
end
def create
#employee = User.new(employee_params)
#employee.company = #current_company
if #employee.save
redirect_to admins_employees_path
else
render :new
end
end
private
def employee_params
params.require(:user).permit(:email, :password, :profile_attributes => [:firstName, :lastName, :title, :fullTime, :startDate])
end
end
Edit.html.erb
<!--BODY-->
<%= render partial: 'form', locals: { employee: #employee, profile_attributes: :profile_attributes } %>
<!--BODY END-->
_form.html.erb
<%= form_for employee, url: admins_employees_path do |f| %>
<div class="col-4 mb-3">
<% if employee.errors.any? %>
<div class="alert alert-danger">
<h4><%= pluralize(employee.errors.count, "error") %>.</h4>
<ul>
<% employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col p-0 mr-3">
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.text_field :password, class: 'form-control' %>
</div>
<%= f.fields_for profile_attributes do |user_f| %>
<div class="form-group">
<label>First Name</label>
<%= user_f.text_field :firstName, :placeholder => 'First Name', class: 'form-control' %>
</div>
<div class="form-group">
<label>Last Name</label>
<%= user_f.text_field :lastName, :placeholder => 'Last Name', class: 'form-control' %>
</div>
<div class="form-group">
Job <%= user_f.label :title %>
<%= user_f.text_field :lastName, :placeholder => 'Title', class: 'form-control' %>
</div>
<div class="form-group">
<label>Employment Start Date</label>
<%= user_f.text_field :startDate, :placeholder => 'Start Date', class: 'form-control' %>
</div>
<% end %>
</div>
<div class="col-12 p-0">
<%= f.submit "Submit", :class => 'btn btn-primary btn-block btn-lg' %>
</div>
</div>
</div>
<% end %>
Thanks!
(EDIT) Routes
Prefix Verb URI Pattern Controller#Action
employees_accounts GET /employees/accounts(.:format) employees/accounts#index
POST /employees/accounts(.:format) employees/accounts#create
new_employees_account GET /employees/accounts/new(.:format) employees/accounts#new
edit_employees_account GET /employees/accounts/:id/edit(.:format) employees/accounts#edit
employees_account GET /employees/accounts/:id(.:format) employees/accounts#show
PATCH /employees/accounts/:id(.:format) employees/accounts#update
PUT /employees/accounts/:id(.:format) employees/accounts#update
DELETE /employees/accounts/:id(.:format) employees/accounts#destroy
admins_accounts GET /admins/accounts(.:format) admins/accounts#index
POST /admins/accounts(.:format) admins/accounts#create
new_admins_account GET /admins/accounts/new(.:format) admins/accounts#new
edit_admins_account GET /admins/accounts/:id/edit(.:format) admins/accounts#edit
admins_account GET /admins/accounts/:id(.:format) admins/accounts#show
PATCH /admins/accounts/:id(.:format) admins/accounts#update
PUT /admins/accounts/:id(.:format) admins/accounts#update
DELETE /admins/accounts/:id(.:format) admins/accounts#destroy
admins_employees GET /admins/employees(.:format) admins/employees#index
POST /admins/employees(.:format) admins/employees#create
new_admins_employee GET /admins/employees/new(.:format) admins/employees#new
edit_admins_employee GET /admins/employees/:id/edit(.:format) admins/employees#edit
admins_employee GET /admins/employees/:id(.:format) admins/employees#show
PATCH /admins/employees/:id(.:format) admins/employees#update
PUT /admins/employees/:id(.:format) admins/employees#update
DELETE /admins/employees/:id(.:format) admins/employees#destroy
registrations GET /registrations(.:format) registrations#index
POST /registrations(.:format) registrations#create
new_registration GET /registrations/new(.:format) registrations#new
edit_registration GET /registrations/:id/edit(.:format) registrations#edit
registration GET /registrations/:id(.:format) registrations#show
PATCH /registrations/:id(.:format) registrations#update
PUT /registrations/:id(.:format) registrations#update
DELETE /registrations/:id(.:format) registrations#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET /
for your information i would like to add that rails is so cleaver that it doesn't need to tell url with form_for , Reason being if the object in form_form is new that it will automatically hit create action on submit and if object is already exist than it will hit update action on submit. thats the beauty of partial and dry concept of rails with form_for so here
you have one partial form which is being used for edit and new action both
just change one line here:- it doesn't need to tell to url each time for new and update action.
<%= form_for employee do |f| %>
//your codes for form
<%end%>
also there is no need to pass nested_attribites local variable, as it will automatic fetch with f object
<%= render partial: 'form', locals: { employee: #employee } %>
but for new action you would need to build nested_attribute object
def new
#employee = User.new
#employee.profile_attributes.build
end
You can remove all this error by changing a bit in your code.
edit.html.erb :
<%= form_for employee, url: admins_employee_path(params[:id]) do |f| %>
<%= render partial: 'form', locals: { employee: #employee, profile_attributes: :profile_attributes, f: f } %>
<% end %>
new.html.erb :
<%= form_for employee, url: admins_employees_path do |f| %>
<%= render partial: 'form', locals: { employee: #employee, profile_attributes: :profile_attributes, f: f } %>
<% end %>
and remove the form line and end from form partial

Devise Recoverable says Email can't be blank on Rails 4/5

Given I am a registered user
And I am not logged in
When I visit "http://localhost:3000/member/password/edit?reset_password_token=BVE492WU1YqMp6nmxCX4"
And I fill in "asdfasdf" in "user[:password]
And I fill in "asdfasdf" in "user[:password_confirmation]
And I submit form
Then I see "Email can't be blank" -> which should be skipped
routes.rb
devise_for :users,
path: 'member',
controllers: {registrations: 'member/registrations',
sessions: 'member/sessions',
passwords: 'member/passwords',
confirmations: 'member/confirmations',
unlocks: 'member/unlocks',
omniauth_callbacks: 'member/omniauth_callbacks'}
devise_scope :user do
match 'member/finish_signup/:id', to: 'member/registrations#finish_signup', via: [:get, :post], :as => :finish_signup
match 'member/show_token', to: 'member/registrations#show_token', via: [:get, :post]
# also tried without the following lines
patch 'member/password' => 'member/passwords#update'
put 'member/password' => 'member/passwords#update'
end
application_controller.rb
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :password])
devise_parameter_sanitizer.permit(:account_update, keys: [:email, :password, :password_confirmation])
# NONE of the both below change anything
devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :password, :password_confirmation])
# this one does not work anymore
# devise_parameter_sanitizer.for(:sign_up) do |u|
# u.permit(:email,:password,:password_confirmation)
# end
end
passwords/edit.html.erb
<%= form_for(resource, as: resource_name, url: password_path(resource_name), method: :put) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em><br/>
<% end %>
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<%= f.submit "Change my password" %>
<% end %>
also tried this:
<%= form_for(resource, as: resource_name, url: member_password_path(resource_name), html: {method: :put}) do |f| %>
The rendered HTML is which looks good (post method):
<form class="new_user" id="new_user" action="/member/password.user" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓"><input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="Egh7QFhtHwcBVCuUgTDFnOF2C/g2Ql/sKeiuwjchjmpDh4JqyBPuLTaN9VT9iFFiP7JDxbp0+BeeA2QUSJWN3g==">
<input type="hidden" value="BVE492WU1YqMp6nmxCX4" name="user[reset_password_token]" id="user_reset_password_token">
<em>(8 characters minimum)</em><br>
<input autofocus="autofocus" autocomplete="off" type="password" name="user[password]" id="user_password">
<input autocomplete="off" type="password" name="user[password_confirmation]" id="user_password_confirmation">
<input type="submit" name="commit" value="Change my password" data-disable-with="Change my password">
</form>
member/passwords_controller.rb
class Member::PasswordsController < Devise::PasswordsController
end
rake routes:
new_user_session GET /member/sign_in(.:format) member/sessions#new
user_session POST /member/sign_in(.:format) member/sessions#create
destroy_user_session GET /member/sign_out(.:format) member/sessions#destroy
user_omniauth_authorize GET|POST /member/auth/:provider(.:format) member/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback GET|POST /member/auth/:action/callback(.:format) member/omniauth_callbacks#(?-mix:facebook)
user_password POST /member/password(.:format) member/passwords#create
new_user_password GET /member/password/new(.:format) member/passwords#new
edit_user_password GET /member/password/edit(.:format) member/passwords#edit
PATCH /member/password(.:format) member/passwords#update
PUT /member/password(.:format) member/passwords#update
cancel_user_registration GET /member/cancel(.:format) member/registrations#cancel
user_registration POST /member(.:format) member/registrations#create
new_user_registration GET /member/sign_up(.:format) member/registrations#new
edit_user_registration GET /member/edit(.:format) member/registrations#edit
PATCH /member(.:format) member/registrations#update
PUT /member(.:format) member/registrations#update
DELETE /member(.:format) member/registrations#destroy
user_confirmation POST /member/confirmation(.:format) member/confirmations#create
new_user_confirmation GET /member/confirmation/new(.:format) member/confirmations#new
GET /member/confirmation(.:format) member/confirmations#show
user_unlock POST /member/unlock(.:format) member/unlocks#create
new_user_unlock GET /member/unlock/new(.:format) member/unlocks#new
GET /member/unlock(.:format) member/unlocks#show
finish_signup GET|POST /member/finish_signup/:id(.:format) member/registrations#finish_signup
member_show_token GET|POST /member/show_token(.:format) member/registrations#show_token
member_password PATCH /member/password(.:format) member/passwords#update
PUT /member/password(.:format) member/passwords#update
And this is the log when I submit the filled out "/passwords/edit.html.erb"
(http://localhost:3000/member/password/edit?reset_password_token=BVE492WU1YqMp6nmxCX4)
Started GET "/member/password/edit?reset_password_token=[FILTERED]" for 127.0.0.1 at 2016-03-04 12:40:41 +0100
Processing by Member::PasswordsController#edit as HTML
Parameters: {"reset_password_token"=>"[FILTERED]"}
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(sign_up) is deprecated and it will be removed from Devise 4.1.
Please use the `permit` method:
devise_parameter_sanitizer.permit(:sign_up) do |user|
# Your block here.
end
(called from deprecate_for_with_block at /Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-4.0.0.rc1/lib/devise/parameter_sanitizer.rb:177)
Rendered devise/shared/_links.html.erb (1.6ms)
Rendered devise/passwords/edit.html.erb within layouts/application (3.5ms)
Completed 200 OK in 14ms (Views: 11.5ms)
Incoming Headers:
Origin: http://localhost:3000
Access-Control-Request-Method:
Access-Control-Request-Headers:
Started POST "/member/password.user" for 127.0.0.1 at 2016-03-04 12:41:19 +0100
Processing by Member::PasswordsController#create as
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gmyAZDVZR4Z/OO3oa934MzNUwCFnzDSd7QLo6ZCoAnjT43lOpSe2rEjhMygXZWzN7ZCIHOv6k2Za6SI/7xwBzA==", "user"=>{"reset_password_token"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Change my password"}
Completed 406 Not Acceptable in 2ms
ActionController::UnknownFormat (ActionController::UnknownFormat):
responders (2.1.1) lib/action_controller/respond_with.rb:207:in `respond_with'
devise (4.0.0.rc1) app/controllers/devise/passwords_controller.rb:19:in `create'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/abstract_controller/base.rb:183:in `process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/action_controller/metal/rendering.rb:30:in `process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/actionpack/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:126:in `call'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:126:in `call'
/Users/jan/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-c901fad42cb4/activesupport/lib/active_support/callbacks.rb:506:in `blo
I think the routing from password_path(resource_name) is incorrect. It brings me to member/passwords#create instead of member/passwords#update which seems to be right.
Please, how can I fix this?
.gemfile
gem 'rails', github: "rails/rails"
gem 'active_model_serializers', github: "rails-api/active_model_serializers", branch: 'master'
gem 'mongoid', git: 'https://github.com/estolfo/mongoid.git', branch: 'MONGOID-4218-rails-5'
# api token authentication for devise
gem 'devise', "4.0.0.rc1"
# we can switch back when Rails 5, and Devise 4 is supported
gem 'simple_token_authentication', github: "consti/simple_token_authentication"
gem 'bootstrap_form'
gem 'omniauth'
gem 'omniauth-facebook'
Changing the as: name and the of the route and the method to post did the trick. It seems that they got not overridden in my devise_scope.
'member/password/update' => 'member/passwords#update', as: :member_password_custom_update

Edit/Update calls the create in ruby on rails

I am stuck in weird problem. I have few recall values that are created and deleted correctly, but when i try to edit the values and save them, it created new recall.
here is my controller for Edit/update
def edit
#recall = Recall.find(params[:id])
end
def update
#recall = Recall.find(params[:id])
if #recall.update_attributes(params[:recall])
# Handle a successful update.
flash[:success] = "Recall updated"
redirect_to '/recalls'
else
render 'edit'
end
end
def show
#user = Recall.find(params[:id])
end
my edit.html.erb is as follows
<%= form_for(#recall) do |f| %>
<%= f.label :Category, "Category" %>
<div class="control-group">
<div class="controls">
<%= f.select :Category,options_for_select(["Consumer Products",
"Foods, Medicines, Cosmetics",
"Meat and Poultry Products",
"Motor Vehicles",
"Child Safety Seats",
"Tires",
"Vehicle Emissions",
"Environmental Products",
"Boats and Boating Safety"]), {:style => "height:40px"} %>
</div>
</div>
<div class="form-inline">
<%= f.label :Title, "Title" %>
</div>
<%= f.text_field :Title %>
<div class="form-inline">
<%= f.label :Summary, "Summary" %>
</div>
<%= f.text_field :Summary %>
<div class="form-inline">
<%= f.label :Details, "Details" %>
</div>
<%= f.password_field :Details %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
please let me know where i did wrong. i tried to define :action => 'edit' but it didn't worked out.
Thanks in advance
EDIT
rake routes output is here
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / administrator_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
about /about(.:format) administrator_pages#about
recalls /recalls(.:format) administrator_pages#Recalls
recall /recall(.:format) administrator_pages#create
destroy /destroy(.:format) administrator_pages#destroy
edit /edit(.:format) administrator_pages#edit
users_new GET /users/new(.:format) users#new
paid_user_paid GET /paid_user/paid(.:format) paid_user#paid
basic_user_basic GET /basic_user/basic(.:format) basic_user#basic
search_Search GET /search/Search(.:format) search#Search
here is my routes.rb, looking at rake routes and my routes.rb i can see something wrong. but unable to firgure out the problem
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'administrator_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/about', to: 'administrator_pages#about'
match '/recalls', to: 'administrator_pages#Recalls'
match 'recall', to:'administrator_pages#create'
match 'destroy', to: 'administrator_pages#destroy'
match 'edit', to: 'administrator_pages#edit'
# get "administrator_pages/Recalls"
get "users/new"
get "paid_user/paid"
get "basic_user/basic"
get "search/Search"
I'm not seeing an update method on your routes, just add to your routes.rb
(if an update method on administrator_pages_controller.rb)
match '/recalls', to: 'administrator_pages#recalls', :as => :recalls
match '/edit/:id', to: 'administrator_pages#edit', :as => :edit_recall
put '/update/:id', to: 'administrator_pages#update'm :as => :update_recall
And run rake routes and you will see looks like
recalls GET /recalls administrator_pages#recalls
edit_recall GET /edit/:id(.:format) administrator_pages#edit
update_recall PUT /update/:id(.:format) administrator_pages#update
http://localhost:3000/recalls recalls action
http://localhost:3000/edit/:id edit action
http://localhost:3000/update/:id update action
Your form edit looks like :
<%= form_for(#recall, :url => update_recall_path(#recall), :html => { :method => :put }) do |f| %>
:url => update_recall_path(#recall) for call update action and using :html => { :method => :put }
Your controller update method
def update
#recall = Recall.find(params[:id])
if #recall.update_attributes(params[:recall])
# Handle a successful update.
flash[:success] = "Recall updated"
redirect_to recalls_path
else
render 'edit'
end
end
recalls_path is after update will redirect into http://localhost:3000/recalls
I have try it on my localhost like your code and it's works. Hope this help.
Started PUT "/update/1" for 127.0.0.1 at 2013-07-02 20:37:14 +0700
Processing by AdministratorPagesController#update as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"s0tVbNt0JedecA+iCVlJ9GmIhGCsf
ltTbb1ep+mZmcY=", "recall"=>{"name"=>"test"}, "commit"=>"Update Recall", "id"=>"1"
}
←[1m←[36mRecall Load (0.0ms)←[0m ←[1mSELECT "recalls".* FROM "recalls" WHERE "recalls"."id" = ? LIMIT 1←[0m [["id", "1"]]
←[1m←[35m (0.0ms)←[0m begin transaction
←[1m←[36m (1.0ms)←[0m ←[1mUPDATE "recalls" SET "name" = 'test', "updated_at" =
'2013-07-02 20:37:14.772915' WHERE "recalls"."id" = 1←[0m
←[1m←[35m (6.0ms)←[0m commit transaction
Redirected to http://localhost:3000/recalls
Completed 302 Found in 13ms (ActiveRecord: 7.0ms)
you need to send an id of a recallwithin your route, because in edit/update actions you do:
#recall = Recall.find(params[:id])
your route for edit should look like this:
match 'edit/:id', to: 'administrator_pages#edit', as: 'edit_recall'
and looks like you'll need one more for update but with method: :put
with the above route you'll have a url like this:
localhost:3000/3/edit #3 is the id of recall
but if you want administrator_pages ahead you'll have to modify your routes:
match 'administrator_pages/recall/edit/:id', to: 'administrator_pages#edit', as: 'edit_recall'
result:
localhost:3000/administrator_pages/recall/3/edit
at the request params of id will be sent and you can use that Recall.find(params[:id]) in your controller. And you'll have to draw a route for update action too with method put
A better solution, I would add resource recalls to routes:
resources :recalls
this would give me all needed routes to work with recall, edit, new, show, etc..
Try the following code
<%= form_for(#recall, :url => recall_path(#recall), :method => 'PUT') do |f| %>

Rails form not using explicit action

I'm a noob that's been struggling with this problem for longer than I care to admit. I used to have a routing issue with my STI model, but now I think that's solved (thanks to SO).
My problem is once I update the form at : /kids/1/edit, instead of having the record saved, it seems to get the record again. I know I'm missing something basic, yet after working the issue a long time the answer eludes me. You can see I'm explicitly calling the kidupdate action with the form submission.
thanks in advance.
kidedit.html.erb
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span5 offset3">
<%= form_for(#kid, url: kidedit_path) do |f| %>
<#%= render 'shared/error_messages', object: f.object %>
<%= f.label :fname, "First Name" %>
<%= f.text_field :fname %>
<%= f.label :lname, "Last Name" %>
<%= f.text_field :lname %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :type, "Are you a Kid or Parent" %>
<%= f.select :type, [['Kid','Kid'],['Parent','Parent']] %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary", :controller => 'users', :action => 'kidupdate' %>
<% end %>
</div>
</div>
users_controller.rb
def kidupdate
#kid = Kid.find(params[:id])
if #kid.update_attributes(params[:kid])
flash[:success] = "Profile updated"
sign_in #kid
redirect_to kidshow_path
else
render kidedit_path(#kid)
end
end
routes.rb
Kidtunes::Application.routes.draw do
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match 'kids/:id' => 'users#kidupdate', via: :put, :as => :kidupdate
match 'kids/:id' => 'users#kidshow', via: :get, :as => :kidshow
match 'kids/:id/edit' => 'users#kidedit', :as => :kidedit
resources :users
resources :sessions, only: [:new, :create, :destroy]
Here's what's in the server log:
Started PUT "/kids/1/edit" for 127.0.0.1 at 2012-11-05 07:52:28 -0500
Processing by UsersController#kidedit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"T8RqFt9lxdbZU+1cOh2E5yu2CFbVRDGmRcj2XdDN1ZU=", "user"=>{"fname"=>"Dante", "lname"=>"Refford", "email"=>"drefford#example.com", "type"=>"Kid", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save changes", "id"=>"1"}
Kid Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."type" IN ('Kid') AND "users"."id" = ? LIMIT 1 [["id", "1"]]
Rendered users/kidedit.html.erb within layouts/application (4.1ms)
Rendered layouts/_shim.html.erb (0.0ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '1RZr3qfB6QSh42_jQ9qNWQ' LIMIT 1
Rendered layouts/_header.html.erb (2.5ms)
Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 67ms (Views: 46.3ms | ActiveRecord: 1.0ms)
Routes
$rake routes
root / static_pages#home
help /help(.:format) static_pages#help
contact /contact(.:format) static_pages#contact
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
kidupdate PUT /kids/:id(.:format) users#kidupdate
kidshow GET /kids/:id(.:format) users#kidshow
kidedit /kids/:id/edit(.:format) users#kidedit
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
As the first comment said, the form should be using the update_path method:
<%= form_for(#kid, url: kidupdate_path) do |f| %>

Resources