编辑用户详细信息创建新用户。 Devise,Rails 4 [英] Editing user details creates new user. Devise, Rails 4
问题描述
我有奇怪的问题,以前创建的表单,而不是更新用户的详细信息,如电子邮件或名称创建新用户给定的细节。
我的表单:
我从 views / devise / registration / 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 | %GT;
< 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ārtotparoli * vismaz 8 simboli,:data-parsley-group=>f3%>
< / div>
< button type =submitclass =blue-button btn btn-default>Apstiprināt< / button>
<%end%>
路线文件
devise_for:users,:controllers => {:注册=> 注册}
注册控制器:
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
在应用帮助器中:
module ApplicationHelper
def resource_name
:user
end
def resource
@resource || = User.new
end
def devise_mapping
@devise_mapping || = Devise .mappings [:user]
end
end
日志文件
启动POST/ ru / users为85.255.65.15在2015-09-28 19:32:25 +0300
处理注册控制器#创建为JS
参数:{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。 `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存在(44.1ms)[0m [1mSE LECT 1一个来自`users`的WHERE`users`.`email` = BINARY'test11 @ !!!'LIMIT 1 [0m
[1m [35mUser Load(24.6ms)] [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`(` confirm_sent_at`,`confirm_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.7ms)
Devise :: Mailer#confirmation_instructions:处理的出站邮件在1726.5ms
发送邮件到test11 @ !!! eu(776?)
日期:星期一,28日p 2015 19:32:31 +0300
从:support@!!!!.eu
回复:support@!!!!.eu
To:test11 @ !!! !.eu
Message-ID:< 56096b9f98eb5_25e4640a718550c6 @ !!!!!>
主题:确认说明
Mime版本:1.0
内容类型:text / html;
charset = UTF-8
Content-Transfer-Encoding:7bit
< p>欢迎!!!< / p>
< p>您可以通过以下链接确认您的帐户电子邮件:< / p>
我的路线:
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格式)devise / password#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)注册#new {:locale => / lv | ee | ru /}
edit_user_registration GET(/:locale)/users/edit(.:format)编辑{:locale => / lv | ee | ru /}
PATCH(/:locale)/users(.:format)注册#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)设计/确认#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_receiv e GET(/:locale)/sms/receive(.:format)sms#receive {:locale => / lv | ee | ru /}
root GET /(:locale)(.:format)girls #index {:locale => / lv | ee | ru /}
我很困惑是否创建此错误的registration_path(resource_name)?
任何建议都可能会有帮助。
提前感谢。
问题是表单,因为它指向 registration_path(resource_name)
路径。
您正在使用的示例取自如何:允许用户编辑他们的帐户而不提供密码,它说:
并提供编辑和更新操作,就像您对任何其他
资源一样在您的应用程序中。
这意味着您需要创建一个到编辑方法的路由,并根据需要使用它。
上面的示例是更新没有密码的用户,但您正在做的是更新密码。
对于解决方案,请阅读如何:允许用户编辑他们的密码
一个示例解决方案 - #3:
UsersController
class UsersController< ApplicationController
before_filter:authenticate_user!
def edit
@user = current_user
end
def update_password
@user = User.find(current_user.id)
如果@ user.update(user_params)
#通过传递验证登录用户,以防其密码更改
sign_in @user,:bypass => true
redirect_to root_path
else
渲染edit
end
end
private
def user_params
#注意:使用`strong_parameters` gem
params.require(:user).permit(:password,:password_confirmation)
end
end
如果您使用多个范围,请指定您正在登录的范围:
sign_in:user,@user,bypass:true
路线应该如下:
resource:user,only:[:edit] do
collection do
patch'update_password'
end
end
查看
code><%= form_for(@user,:url => {:action =>update_password})do | f | %GT;
< div class =field>
<%= f.label:password,密码%>< br />
<%= f.password_field:password,:autocomplete => 关%>
< / div>
< div class =field>
<%= f.label:password_confirmation%>< br />
<%= f.password_field:password_confirmation%>
< / div>
< div class =action_container>
<%= f.submit%>
< / div>
<%end%>
要使用confirm_password字段强制用户输入旧密码
在更新之前用新的更改:将@ user.update(user_params)
更改为
@ user.update_with_password(user_params)
在
中添加$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ current $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
< div class =field>
<%= f.label:current_password%> < i(我们需要您当前的密码来确认您的更改)< / i>< br />
<%= f.password_field:current_password%>
< / div>
记住,Devise模型与您的应用程序中的任何模型一样。如果
想要提供自定义行为,只需实现新的操作和新的
控制器。不要试图弯曲Devise。
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.
这篇关于编辑用户详细信息创建新用户。 Devise,Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!