无法编辑用户个人资料

时间:2014-04-18 22:20:48

标签: ruby-on-rails ruby-on-rails-4

当我尝试编辑用户信息时,点击“更新”后没有任何反应,除了最后显示真实性令牌的网址。日志显示未允许的参数错误,如下所示:

 Unpermitted parameters: utf8, _method, authenticity_token, user, commit, format

我更改了设计路线以添加omniauth注册以及用于单独编辑视图的自定义设计控制器。 路线:

devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
               controllers: {omniauth_callbacks: "omniauth_callbacks"}

devise_scope :user do
get "/info" => "registrations#info"
end

这是自定义设备控制器:

class RegistrationsController < Devise::RegistrationsController
def info
    @user = current_user
    if @user 
        render :info
    else
        redirect_to root_path
     end 
    end

   def update
    @user = User.find(params[:id])


  @user.update_without_password(devise_parameter_sanitizer.for(:account_update))
 redirect_to user_path(current_user)

 end
 protected

 def after_sign_up_path_for(resource)
   '/info'
  end
 end

这是编辑视图:

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

 <%= f.input :name %>
 <%= f.input :email %>

<% end %>

1 个答案:

答案 0 :(得分:1)

根据聊天会话,OP有两个forms

<form class="form-horizontal"> 
<form accept-charset="UTF-8" action="/users" class="simple_form user" enctype="multipart/form-data" id="edit_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="om7WhULk2OPgMGwjbTz5h79BqUlkr4lF9aRVDaOxUhs=" /></div> 

建议删除<form class="form-horizontal">并将该类添加到现有simple_form_for,如下所示:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put}, :class =>"form-horizontal") do |f| %>

此外,要更新没有密码的用户记录,请按以下更改更新操作:

  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to user_path(current_user)
    else
      render "edit"
    end
  end

<强>更新

您当前的通话将转到&#34;由Devise处理:: RegistrationsController #news as HTML&#34; Devise::RegistrationsController代替您的RegistrationsController 更新routes.rb,如下所示:

devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, 
controllers: {omniauth_callbacks: "omniauth_callbacks", registrations: :registrations}