设计路由和错误:保存没有current_password的用户参数

时间:2012-11-17 21:20:00

标签: ruby-on-rails devise

我的设计问题。 我想保存没有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

我在我的应用程序中观看了railscast并添加了Registrations控制器

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错误禁止此用户被保存:当前密码不能为空

1 个答案:

答案 0 :(得分:0)

  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