1

私が最初のRails(さらに言えばRuby)アプリケーションを開発する際に対処している新しい問題。

私は(その特定の順序で)使用しています:Devise、Registration、User

登録スキャフォールディング/モデルを生成した後、ファイルを devise/views/registrations から /views/registrations に移動しました。

User スキャフォールディングを生成した後、edit.html.erb と show.html.erb を /views/users に移動しました。

私は特定の管理コントローラーを使用していませんが、current_user の管理者権限をチェックするコードがいくつかあります。その場合、パスワードを入力することなく、管理者がユーザーの情報を編集できるようにしたいと考えています。そのため、編集用に表示しているフォームは、current_user が管理者でない場合 (およびその部分が適切に機能している場合)、パスワード フィールドをスキップします。

編集フォームは正常に表示され、フォームのフィールドには編集中のユーザー (current_user ではない) の情報が入力されます。

私が抱えている問題は、更新を押すと、パスワードが見つからないなどのエラーメッセージが表示されることです.

これは、フォームの編集アクション (およびおそらく他の何か) と関係があると思われます。

edit.html.erb のフォーム宣言については次のとおりです (これは、Devise によって最初に作成されたときの元のフォーム宣言であり、ファイルを登録に移動し、次にユーザー ビューに移動すると、ファイルと共に移動しました):

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

firebug を使用してフォームの HTML を表示すると、次のように表示されます。

<form id="new_user" class="new_user" method="post" action="/" accept-charset="UTF-8">

registration_path を edit_user_path に変更した場合:

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

これは私がHTMLで得るものです:

<form id="new_user" class="new_user" method="post" action="/users/user/edit" accept-charset="UTF-8">

さらに、ここでフォームを送信しようとすると、次のように表示されます。

No route matches [PUT] "/users/user/edit"

私の users_controller.rb には、次のものがあります (登録コントローラーから edit メソッドと update メソッドを削除しました)。

def edit
  @user = User.find(params[:id])
end
def update
  respond_to do |format|
    if @user.update_attributes(params[:user])
      format.html { redirect_to root_url, flash[:notice] = SUCCESSFUL_REGISTRATION_UPDATE_MSG }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

参考: ルートを生成すると、次のように表示されます。

           edit_user GET    /users/:id/edit(.:format)      users#edit
                user GET    /users/:id(.:format)           users#show
                     PUT    /users/:id(.:format)           users#update
cancel_user_registration GET    /cancel(.:format)              registrations#cancel
   user_registration POST   /                              registrations#create
   new_user_registration GET    /request_invite(.:format)      registrations#new
  edit_user_registration GET    /edit(.:format)                registrations#edit
                         PUT    /                              registrations#update

何か案は?

解決

私の場合、更新を使用しているため、解決策は update_without_password を使用することです。代わりに、これが新規用であり、パスワードを要求したくない場合は、次のようになります: save_without_password

def update
  @user = User.find(params[:id])
  respond_to do |format|
    if @user.update_without_password(params[:user])
      format.html { redirect_to root_url, flash[:notice] = SUCCESSFUL_REGISTRATION_UPDATE_MSG }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
   end
end  
4

1 に答える 1

0

デバイスがロジックを処理しているという理由だけで、ユーザーモデルは特別なものではないことに注意してください。ユーザー プロファイルを編集するための新しいコントローラー/ビューを簡単に作成し、管理者にアクセス権を付与することもできます。(これは私がしていることです。ユーザーがパスワードを入力して変更を加える必要があるのは好きではありません)

$ rails g controller users
resources :users, :only => [:edit, :update]

その後は、コントローラー/ビューとほとんど同じです

于 2012-06-07T04:03:29.900 に答える