Ruby on rails 使用Omniauth和Desive为用户登录Rails应用程序创建密码
用户可以使用omniauth提供程序登录到我的Rails应用程序,但我只想为具有密码的用户启用某些功能 使用omniauth凭据创建用户时,其密码为空。当我尝试为omniauth用户创建密码时,会出现错误“当前密码不能为空” 如何为当前密码为“nil”的omniauth用户创建密码? 这是我目前用于编辑用户密码的表单:Ruby on rails 使用Omniauth和Desive为用户登录Rails应用程序创建密码,ruby-on-rails,devise,omniauth,Ruby On Rails,Devise,Omniauth,用户可以使用omniauth提供程序登录到我的Rails应用程序,但我只想为具有密码的用户启用某些功能 使用omniauth凭据创建用户时,其密码为空。当我尝试为omniauth用户创建密码时,会出现错误“当前密码不能为空” 如何为当前密码为“nil”的omniauth用户创建密码? 这是我目前用于编辑用户密码的表单: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<% if current_user.password != nil %>
<div class="formField"><%= f.label :current_password, :class=>"label label-info " %> <br />
<%= f.password_field :current_password %></div>
<% else %>
<%= f.hidden_field :current_password, :value => nil %>
<% end %>
<div class="formField"><%= f.label "New password", :class=>"label label-info" %> <br />
<%= f.password_field :password, :autocomplete => "off" %>
</div>
<div class="formField"><%= f.label "New password confirmation", :class=>"label label-info" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="editAccountButtons">
<%= f.submit "Update", :class=>"btn btn-small btn-info submitButton" %>
</div>
<% end %>
这个问题回答了我的问题 这是我的最终控制员:
def update
@user = User.find(current_user.id)
force_updated = false
successfully_updated = if needs_password?(@user, params)
Rails.logger.debug('updating with password')
@user.update_with_password(params[:user])
else
if resource.update_attributes(params[resource_name])
force_updated = true
end
end
if successfully_updated || force_updated
if params[:update_email]
set_flash_message :alert, :signed_up_but_unconfirmed
redirect_to after_update_path_for(@user)
else
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
end
else
redirect_to :back, alert: resource.errors.full_messages[0]
end
end
def update
@user = User.find(current_user.id)
force_updated = false
successfully_updated = if needs_password?(@user, params)
Rails.logger.debug('updating with password')
@user.update_with_password(params[:user])
else
if resource.update_attributes(params[resource_name])
force_updated = true
end
end
if successfully_updated || force_updated
if params[:update_email]
set_flash_message :alert, :signed_up_but_unconfirmed
redirect_to after_update_path_for(@user)
else
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
end
else
redirect_to :back, alert: resource.errors.full_messages[0]
end
end