【问题标题】:Routes and error with devise: Save user parameters without current_password路线和设计错误:保存没有 current_password 的用户参数
【发布时间】:2012-11-17 21:20:00
【问题描述】:

我的设计问题。 我想在没有 current_password 的情况下保存用户的私人数据。 我想删除一些路线,只留下必要的。

routes.rb

  devise_for :users, :controllers => { :registrations => "registrations" }, :skip => [:registrations]
    as :user do
      post "/users" => "devise/registrations#create", :as => :user_registration
      get "/users" => "devise/registrations#new", :as => :new_user_registration
      get "/users/edit" => "devise/registrations#edit", :as => :edit_user_registration
      put "/users" => "devise/registrations#update"
    end

我观看了 railscasts 并在我的应用程序中添加了注册控制器

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def update
    @user = User.find(current_user.id)
   # email_changed = @user.email != params[:user][:email]
    password_changed = !params[:user][:password].empty?

    successfully_updated = if password_changed
      @user.update_with_password(params[:user])
    else
      @user.update_without_password(params[:user])
    end

    if successfully_updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end
end

我的观点edit.html.erb

<h2><%= t('users.edit_registration_data') %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>

  <ul class="nav nav-tabs">
      <li class="active" ><a href="#profile" data-toggle="tab"><%= t('tabs.private_info') %></a></li>
      <li><a href="#password" data-toggle="tab"><%= t('tabs.change_password') %></a></li>
  </ul>
  <div class="tab-content">
      <div class="tab-pane active" id="profile">
        <div><%= f.label :name, t('form.name') %><br />
        <%= f.text_field :name %></div>
        <div><%= f.label :phone_number, t('form.phone_number') %> <%= t('form.phone_notice' )%><br />
        <%= f.text_field :phone_number, :maxlength => 10 %></div>
        <div><%= f.label :area_id, t('form.area') %><br />
        <%= f.text_field :area_id %></div>
        <div><%= f.label :city_id, t('form.city') %><br />
        <%= f.text_field :city_id %></div>
        <div><%= f.label :email %><br />
        <%= f.email_field :email %></div>
      </div>
      <div class="tab-pane" id="password">
        <div><%= f.label :password, t('users.form.new_password') %> <i>(<%= t('users.notifications.leave_password') %>)</i><br />
        <%= f.password_field :password, :autocomplete => "off" %></div>

        <div><%= f.label :password_confirmation, t('users.form.new_password_confirmation') %><br />
        <%= f.password_field :password_confirmation %></div>

        <div><%= f.label :current_password, t('users.form.current_password') %> <i>(<%= t('users.notifications.need_current_password') %>)</i><br />
        <%= f.password_field :current_password %></div>
      </div>
  </div>

 <br><br><br>  

  <div><%= f.submit t('users.update') %></div>
<% end %>

<%= link_to t('users.back'), :back %>

但是当我更新任何数据时,除了密码,应用程序仍然要求输入当前密码。

控制器:设计/注册 行动:更新 1 个错误禁止保存此用户:当前密码不能为空

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:
  devise_for :users, :skip => [:registrations]
    as :user do
      post "/users" => "devise/registrations#create", :as => :user_registration
      get "/users" => "devise/registrations#new", :as => :new_user_registration
      get "/users/edit" => "devise/registrations#edit", :as => :edit_user_registration
      put "/users" => "registrations#update"
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    相关资源
    最近更新 更多